Getting PowerShell::Create.AddScript.Invoke return value in c++
See the question and my original answer on StackOverflowYou need to either import the proper namespaces, like this:
#include <Windows.h>
#include <vcclr.h>
#include <iostream>
#include <vector>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Management.Automation.dll>
using namespace std;
using namespace System;
using namespace System::Collections::ObjectModel; // you need this one
using namespace System::Management::Automation;
void RunPowerShell()
{
// or simply use C++ auto!
Collection<PSObject^>^ powObject = PowerShell::Create()->AddScript(gcnew String("get-wmiobject -query \"select * from win32_bios\""))->Invoke();
for (int i = 0; i < powObject->Count; i++)
{
Console::WriteLine(powObject[i]);
}
}
int main()
{
RunPowerShell();
return 0;
or just use the cool C++ auto
keyword, like this:
auto powObject = PowerShell::Create()->AddScript(gcnew String("get-wmiobject -query \"select * from win32_bios\""))->Invoke();
And as a bonus, Visual Studio's magic will show the namespace for you if you hover the mouse around the auto
keyword: