When I run the following command via the PowerShell console, it works just fine:
Get-ProcessMitigation -Name iexplore.exe | Select-Object -ExpandProperty ASLR | Select-Object -ExpandProperty BottomUp
However, if I run the same command within a C# method like the following:
public static List<PSObject> RunLocalPowerShellScript(string p_script)
{
using var psInstance = PowerShell.Create();
psInstance.AddScript(p_script);
IEnumerable<PSObject> commandOutput = null;
try
{
commandOutput = psInstance.Invoke();
}
catch (Exception e)
{
var exc = e.Message;
}
if (psInstance.HadErrors)
{
var errorOutput = string.Join(Environment.NewLine, psInstance.Streams.Error.Select(e => e.ToString()));
}
return commandOutput.ToList();
}
and pass the command as the argument, it fails (note the above code needs C# 8 to run properly, but wrapping it in a using
block C# 7-style causes the same issue).
The error says This script must be run on Windows 10 or greater.
. The issue being that I'm running on Windows 10 1903. It happens both in a .NET Framework project using the Microsoft.PowerShell.5.ReferenceAssemblies
package from NuGet, and in .NET Core 3 using the Microsoft.PowerShell.SDK
package from NuGet. Other PowerShell commands run without any issue using the exact same method, and the same command runs without issues in a slightly tweaked remote version of our method where we set the remote runspace to run against remote machines. This problem seems to only be limited to running on the local machine.
Does anyone have any insight on this issue? It wouldn't be the first bug we've found at the office in the Get/Set-ProcessMitigation modules, but I figure it's always good to get some outside eyes looking at things.