【发布时间】:2019-05-09 01:09:38
【问题描述】:
我正在尝试将数据从 PowerShell 检索到 c# 对象中。我要查找的数据是从远程 Web 服务器上的 GetExecutingRequests 的 PowerShell Invoke() 返回的。我遇到的问题是我没有收到错误代码,但我正在寻找的 Invoke() 的结果在返回数据或 PowerShell 对象中不存在。
using (Runspace runspace = RunspaceFactory.CreateRunspace(cxn))
{
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
string script = String.Format("Get-WmiObject
WorkerProcess -Namespace root\\WebAdministration -ComputerName {0} |
Invoke-WmiMethod -Name GetExecutingRequests", server);
ps.AddScript(script);
ps.AddParameter("OutputElement", new HttpRequest[0]);
var result = ps.Invoke();
}
}
此代码执行,并返回一个包含 29 个项目的集合。但是,它们都没有显示 GetExecutingRequests 结果,并且在 PowerShell 对象上也没有任何相关内容。
我想将 GetExecutingRequests 的输出转换为 c# 对象,以便进行进一步处理。 ps 对象上的 PSDataStreams 也没有结果。
任何帮助将不胜感激。
更多信息:
我能够通过更改我发送的 PowerShell 脚本来解决这个问题:
string script = String.Format("Get-WmiObject WorkerProcess -Namespace root\\WebAdministration -ComputerName {0} | Invoke-WmiMethod -Name GetExecutingRequests | %{{ $_ | Select-Object -ExpandProperty OutputElement }}", server);
【问题讨论】:
标签: c# powershell