我是.net的新手。将powershell命令输出到控制台(在c#中调用)时遇到问题。
代码:
PowerShell powershellCommand = PowerShell.Create();
powershellCommand.AddScript("get-process");
Collection<PSObject> results = powershellCommand.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(results);
}
Console.Read();输出: System.Collections.ObjectModel.Collection`1System.Management.Automation.PSObject
发布于 2019-03-15 16:15:36
您正在迭代您的集合,但您编写的不是当前项,而是整个集合。你必须写下这个项目:
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();https://stackoverflow.com/questions/55177977
复制相似问题