I was wondering how to write code in c++ that could send commands to powershell. I'd like to be able to parse the output as well but at this point I'd just like to begin learning how to run commands and can learn to parse the output later. Thank you ahead of time to anyone able to help.
-
Check out these other threads. They are c# based, but the concept is the same. stackoverflow.com/questions/13251076/… stackoverflow.com/questions/17067971/…Jim– Jim2013-10-28 12:45:06 +00:00Commented Oct 28, 2013 at 12:45
-
Here is another forum post with helpful links: forums.iis.net/t/1200231.aspxJim– Jim2013-10-28 12:52:05 +00:00Commented Oct 28, 2013 at 12:52
-
Are you willing to write C++/CLI code because that is what you're going to need to do. The PowerShell engine has a .NET API.Keith Hill– Keith Hill2013-10-28 14:16:11 +00:00Commented Oct 28, 2013 at 14:16
Add a comment
|
1 Answer
Here's a small sample that should get you going:
#include "stdafx.h"
using namespace System;
using namespace System::Collections::ObjectModel;
using namespace System::Management::Automation;
int _tmain(int argc, _TCHAR* argv[])
{
PowerShell^ _ps = PowerShell::Create();
_ps->AddScript("Get-ChildItem C:\\");
auto results = _ps->Invoke();
for (int i = 0; i < results->Count; i++)
{
String^ objectStr = results[i]->ToString();
Console::WriteLine(objectStr);
}
return 0;
}
Make sure the /clr switch is enabled for your C++ project.
1 Comment
ShresthaGanesh
what is the significance of the ^ sign at the end of each class including PowerShell^ and String^?