Reputation: 185
i'm new to .net. Fall into a problem with outputing powershell command into console, invoked in c#.
Code:
PowerShell powershellCommand = PowerShell.Create();
powershellCommand.AddScript("get-process");
Collection<PSObject> results = powershellCommand.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(results);
}
Console.Read();
Output: System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]
Upvotes: 0
Views: 974
Reputation: 14007
You are iterating over your collection, but you are not writing the current item, but the collection as a whole. You have to write the item:
PowerShell powershellCommand = PowerShell.Create();
powershellCommand.AddScript("get-process");
Collection<PSObject> results = powershellCommand.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(result); //<-- result NOT results
}
Console.Read();
Upvotes: 1