C ++和Powershell [英] C++ and Powershell

查看:271
本文介绍了C ++和Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在c ++中编写可以向powershell发送命令的代码。我想要能够解析输出,但在这一点上,我只想开始学习如何运行命令,并可以学习解析输出以后。

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.

推荐答案

这里有一个小样本,应该可以帮助你:

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;
}

确保 / clr 开关为您的C ++项目启用。

Make sure the /clr switch is enabled for your C++ project.

这篇关于C ++和Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆