简体   繁体   English

使用 Powershell SDK 和 C# 启用 IIS

[英]Enable IIS using Powershell SDK and C#

I am trying to enable IIS using Poweshell SDK in C sharp.我正在尝试在 C 中使用 Poweshell SDK 启用 IIS。 My code is as follows.我的代码如下。

using (PowerShell PowerShellInst = PowerShell.Create())
            {
              
                PowerShellInst.AddScript("Set-ExecutionPolicy Bypass -Scope Process; Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole");
                Collection<PSObject> PSOutput = PowerShellInst.Invoke();

                if (PowerShellInst.HadErrors)
                {

                    foreach (var error in PowerShellInst.Streams.Error)
                    {
                        Console.WriteLine(error.ToString());
                    }
                }
                else
                {
                    foreach (PSObject obj in PSOutput)
                    {
                        if (obj != null)
                        {
                            Console.Write(obj);
                           

                        }
                    }
                }

                PowerShellInst.Stop();
            }
        }

Output is Output 是

 Microsoft.Dism.Commands.ImageObject

When I execute same command using powershell then output is as follows当我使用 powershell 执行相同的命令时,output 如下

在此处输入图像描述

在此处输入图像描述

Is there a way to get output like this?有没有办法像这样获得 output ?

Note: google cloud shell when installing or upgrading on windows via command line shows such output.注意:通过命令行在 windows 上安装或升级时,google cloud shell 会显示 output。

You should use PSObject properties to access to that information.您应该使用 PSObject 属性来访问该信息。 You can iterate through PSObject.Properties like this.您可以像这样遍历PSObject.Properties

foreach (var property in obj.Properties)
{
    Console.WriteLine(property.Name + " : " + property.Value);
}

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

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