下面是powershell:
$app = Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root/SMS/site_sitename -
ComputerName computername | Select-Object User, Application, RequestGUID
$app它工作良好,返回的信息没有问题。
在c#中运行:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell powerShell = PowerShell.Create();
powerShell.Runspace = runspace;
powerShell.AddScript(script);
Collection<PSObject> results = powerShell.Invoke();
foreach (PSObject result in results)
{
MessageBox.Show(result.ToString());
}
runspace.Close();这显示了baseObject,也就是UserApplicationRequest,但是如何访问请求中的数据呢?(这是选择对象用户,应用程序,RequestGUID)
发布于 2013-08-17 20:29:27
如果您使用的是PowerShell V3 (System.Management.Automation.dll 3.0),请不要忘记它现在位于DLR上。这意味着PSObject可以通过dynamic关键字在C#中使用,例如:
foreach (dynamic result in results)
{
var msg = String.Format{"User: {0}, Application: {1}, RequestGUID: {2}",
result.User, result.Application, result.RequestGUID);
MessageBox.Show(msg);
}发布于 2013-08-16 20:50:14
为了获得由Select-Object cmdlet创建的自定义对象,可以迭代Properties成员:
foreach (var result in results)
{
foreach ( var property in result.Properties )
{
MessageBox.Show( string.Format( "name: {0} | value: {1}", property.Name, property.Value ) );
}
}https://stackoverflow.com/questions/18280977
复制相似问题