简体   繁体   English

脚本通过 PowerShell 控制台正确运行,通过 C# 运行时失败

[英]Script runs correctly via PowerShell Console, fails when running via C#

When I run the following command via the PowerShell console, it works just fine:当我通过 PowerShell 控制台运行以下命令时,它工作得很好:

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:但是,如果我在 C# 方法中运行相同的命令,如下所示:

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).并将命令作为参数传递,它会失败(请注意,上面的代码需要 C# 8 才能正常运行,但是将其包装在using块 C# 7 样式中会导致相同的问题)。

The error says This script must be run on Windows 10 or greater.错误提示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. 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.其他 PowerShell 命令使用完全相同的方法运行没有任何问题,并且相同的命令在我们方法的稍微调整的远程版本中运行没有问题,我们将远程运行空间设置为针对远程计算机运行。 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.这不会是我们在办公室的 Get/Set-ProcessMitigation 模块中发现的第一个错误,但我认为让一些外人的眼睛看东西总是好的。

Seems the Get-ProcessMitigation cmdlet checks the OS version before executing.似乎Get-ProcessMitigation cmdlet 在执行之前会检查操作系统版本。 This would be reasonable, as it might need a new library or is dealing with some new features, but the problem is that by default your app is being told it is running on Windows 8 (6.2), even when the OS is really Windows 10:这是合理的,因为它可能需要一个新库或正在处理一些新功能,但问题是默认情况下,您的应用程序被告知它在 Windows 8 (6.2) 上运行,即使操作系统真的是 Windows 10 :

Targeting your application for Windows 针对 Windows 的应用程序

To get around this, add an app.manifest to your project and uncomment this section:为了解决这个问题,将 app.manifest 添加到您的项目并取消注释此部分:

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM