gpt4 book ai didi

c# - Powershell 到 C# - Get-WMIObject

转载 作者:行者123 更新时间:2023-11-30 17:03:44 26 4
gpt4 key购买 nike

所以这是 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)

最佳答案

如果您使用的是 PowerShell V3 (System.Management.Automation.dll 3.0),请不要忘记它现在位于 DLR 上。这意味着可以通过 C# 中的 dynamic 关键字使用 PSObject,例如:

foreach (dynamic result in results)
{
var msg = String.Format{"User: {0}, Application: {1}, RequestGUID: {2}",
result.User, result.Application, result.RequestGUID);
MessageBox.Show(msg);
}

关于c# - Powershell 到 C# - Get-WMIObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18280977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com