Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

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

enter image description here

enter image description here

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.

question from:https://stackoverflow.com/questions/65650340/enable-iis-using-powershell-sdk-and-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...