【发布时间】:2011-10-01 22:28:21
【问题描述】:
我正在尝试使用托管 C++ 函数中的 PowerShell 类执行 WMI 函数。
但我不知道如何调用从 PowerShell.Invoke() 方法返回的 PSObject 列表中的对象的方法。
(在命令行上我只会做 (gwmi ....).RequestStateChange(2) - 但我看不到如何使用 PowerShell 类的少数方法添加 ()。
System::Management::Automation::PowerShell ^ ps = System::Management::Automation::PowerShell::Create();
ps->AddCommand("Get-WMIObject");
ps->AddParameter("namespace", "root/virtualization");
p->AddParameter("class", "Msvm_ComputerSystem");
// we could add a filter to only return the VM in question but
// I had problems with quoting so choose the
// simplier route.
System::Collections::ObjectModel::Collection<System::Management::Automation::PSObject^>^ result = ps->Invoke();
System::String ^s = gcnew System::String( id.c_str() );
for (int i = 0; i < result->Count; i++ ) {
if ( System::String::Compare( dynamic_cast<System::String ^>(result[i]->Members["Name"]->Value), s) == 0 ) {
// Now what ? I want to call the RequestStateChange method on this VM
return;
}
}
【问题讨论】:
标签: .net c++ powershell wmi