C#调用powershell输出格式为什么不一样?
打开POWERSHELL,输入get-process输出格式是:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
128 5 2148 4852 49 0.17 2824 atieclxx
121 4 1512 4052 28 0.08 856 atiesrxx
125 5 15732 15680 42 0.23 5344 audiodg
。。。。。
C#调用ps代码:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command getProcess = new Command(@"c:\test.ps1",true);
pipeline.Commands.Add(getProcess);
Collection<PSObject> output = pipeline.Invoke();
foreach (PSObject obj in output)
{
Console.WriteLine(obj.ToString());
}
Console.ReadKey();
输出结果如下
System.diagnostics.process (svcgist)
System.diagnostics.process ....
这是什么原因呢?谢谢。
------解决方案--------------------你最后的取值方式不对,应该像下面这样子:
foreach (PSObject obj in output)
{
string ProcessName = obj.Properties["ProcessName"].Value.ToString();
string PM = obj.Properties["PM"].Value.ToString();
}
Properties里面的才是你需要的