简体   繁体   English

Get-WindowsOptionalFeature 从 C# 失败

[英]Get-WindowsOptionalFeature failing from C#

I am trying to check if windows server 2012 has IIS installed.我正在尝试检查 windows 服务器 2012 是否安装了 IIS。

I am using following program in C#.我在 C# 中使用以下程序。 It is working fine in windows 7 / 10 but giving error in windows server 2012.它在 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;
}

Error is as follows错误如下

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

when I execute the command from powershell in windows server 2012 it works, only when I execute uing C# it fails当我在 windows 服务器 2012 中执行来自 powershell 的命令时它有效,只有当我执行使用 C# 时它才会失败

PowershellCommand Powershell命令

PowerShell version 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

Please let me know if needs to be changed请让我知道是否需要更改

Open the IIS, right click on the applications application pool and go to "advanced settings" and change "Enable 32-Bit Applications" to "TRUE".打开IIS,右键点击应用程序池,go为“高级设置”,将“启用32位应用程序”改为“真”。 Restart your website and it should work.重新启动您的网站,它应该可以工作。

Or In Visual Studio, Right Click your project -> On the left pane click the Build tab, under Platform Target select x86.或者在 Visual Studio 中,右键单击您的项目 -> 在左侧窗格中单击构建选项卡,在平台目标 select x86 下。

On Desktops OSs use在桌面操作系统上使用

Get-WindowsOptionalFeature -Online

On Servers OSs use在服务器操作系统上使用

Get-WindowsFeature *IIS*

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

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