Reputation: 1675
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.
Upvotes: 1
Views: 105
Reputation: 34
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);
}
Upvotes: 1