从 C# 中获取 WindowsOptionalFeature 失败

Get-WindowsOptionalFeature failing from C#

我正在尝试检查 windows 服务器 2012 是否安装了 IIS。

我在 C# 中使用以下程序。它在 windows 7 / 10 中运行良好,但在 windows 服务器 2012 中出现错误。

public static String ExecutePoswershellScript(String filepath)
{

    log.Info(filepath);

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "powershell.exe";
    startInfo.Arguments = filepath;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();

    string output = process.StandardOutput.ReadToEnd();
    log.Info(output);
    string err = process.StandardError.ReadToEnd();

    if (err.Length>0)
    {
        log.Error(err);
    }
   
   // log.Info(output + "");
    process.WaitForExit();
    process.Close();
    return output;
}

错误如下

Get-WindowsOptionalFeature : An attempt was made to load a program with an incorrect format.
At line:1 char:64
+ ... ocess; Import-Module Dism; Get-WindowsOptionalFeature -Online | where ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WindowsOptionalFeature], CO
MException
    + FullyQualifiedErrorId : Microsoft.Dism.Commands.GetWindowsOptionalFeatureC
ommand

当我在 windows 服务器 2012 中从 powershell 执行命令时它有效,只有当我使用 C# 执行时它失败

PowershellCommand

PowerShell 版本

Name                           Value
----                           -----
PSVersion                      5.0.10586.117
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.10586.117
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

如果需要更改请告诉我

打开IIS,右击应用程序应用程序池,进入“高级设置”,将“启用32位应用程序”改为“TRUE”。重新启动您的网站,它应该可以正常工作。

或者在 Visual Studio 中,右键单击您的项目 -> 在左侧窗格中单击平台目标下的构建选项卡 select x86。

在桌面操作系统上使用

Get-WindowsOptionalFeature -Online

在服务器操作系统上使用

Get-WindowsFeature *IIS*