如何使用C ++获取powershell的命令输出? [英] How to fetch output of command of powershell with C++?

查看:1874
本文介绍了如何使用C ++获取powershell的命令输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在powershell的帮助下实施了网络统计程序 scrpit 。程序运行成功,并给我perfact输出。

i implemented program of network statistics with help of powershell scrpit. the program is running successfully and giving me perfact output as well . below is my program.

int main(int argc, char *argv[])
{
       string strPath = "C:\\Get-NetworkStatistics.ps1";
      char str[50] =  "C:\\Get-NetworkStatistics.ps1";

       char command[500];

//access function:
       //The function returns 0 if the file has the given mode.
       //The function returns –1 if the named file does not exist or does not have the given mode
       if(access(strPath.c_str(),0) == 0)
       {


_snprintf(command, sizeof(command), "Start Powershell.exe -noexit Set -executionpolicy;.'%s';Get-NetworkStatistics",str);
system(command);



       }

       else 
       {
              system("cls");
              cout << "File is not exist";
              system("pause");
        }
return 0;
}

以下是上述计划的输出

你可以看到输出是在powershell窗口..我想获取powershell输出的所有这些数据,并希望在控制台中显示它。

as you can see the output is in the powershell windows.. i want to fetch all this data of powershell output and want to display it in console. how should it possible?

推荐答案

除非您需要在信息可用时实时显示信息,只需将其重定向到一个文件,然后从C ++中读取该文件。

Unless you need to do display the info in realtime as it becomes available, just redirect it to a file, then read that file from C++.

由于

但是它可以 只是在Windows XP SP 2或者我可以使用Powershell来了解。netstat netstat 将满足您的需要,然后您不必处理任何并发症。

But it may just be that netstat will serve your needs, and then you don't have to deal with any of that complication.

顺便说一下,我建议使用脚本语言来编写脚本任务。当然,Powershell脚本默认情况下是禁用的复杂性,否则使用Powershell脚本设施将被指示。但是例如。在这种情况下,[cmd.exe]批处理文件将比从C ++做它的工作更自然。

By the way, I recommend using a scripting language for scripting tasks. There is of course the complication that Powershell scripting is disabled by default, otherwise using the Powershell scripting facility would be indicated. But e.g. in this case a [cmd.exe] batch file would be more natural than doing its job from C++.

可从JScript和VBScript获得的Windows Script Host外壳对象提供了使用输出抓取的过程执行功能。

The Windows Script Host shell objects, available from JScript and VBScript, provide functionality for process execution with output grabbing.

有一个小小的障碍,你必须轮询输出,但我认为它仍然比在C / C ++ API级别更容易。

There is a little snag in that you then have to poll the output, but I think it's still easier than doing this at the C/C++ API level.

这篇关于如何使用C ++获取powershell的命令输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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