English 中文(简体)
如何从管理的C++中援引购买力平价法
原标题:How to invoke method on PSObject from managed C++

I m试图利用C++管理功能的PowerShell级履行WMI职能。

但是,我可以就如何在从PowerShell返回的PSObject名单中标出的物体上打上一种方法。 Invoke()方法。

(关于指挥线,我只做(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;
    }
}
问题回答

Why do you want to us PowerShell to query WMI you can use managed class ManagementObjectSearcher for that :

ManagementObjectSearcher ComputerInfos = new ManagementObjectSearcher("select * from Win32_ComputerSystem");

I know this is a bit stale, but I had similar problem in C# and found this topic as only one describing my problem. The solution I got is pretty basic, which is no wonder since I am beginner with PowerShell. I hope this would answer this problem as well to anyone that may stumble here.

PSObject has .BaseObject property that is used to access underlying Object. 因此,如果你知道哪类物体是预想的方法(你可能这样做,否则我不敢肯定你会怎样期望任何具体方法),那么你就可以干.。

SomeClass x = result[i].BaseObject as SomeClass;
if (x == null) 
{
   //some handling
}
x.SpecificMethod();

This is C# casting, but you get the idea.

Hope this helps.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签