简体   繁体   中英

Enable IIS using Powershell SDK and C#

I am trying to enable IIS using Poweshell SDK in C sharp. 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

 Microsoft.Dism.Commands.ImageObject

When I execute same command using powershell then output is as follows

在此处输入图像描述

在此处输入图像描述

Is there a way to get output like this?

Note: google cloud shell when installing or upgrading on windows via command line shows such output.

You should use PSObject properties to access to that information. You can iterate through PSObject.Properties like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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