从C++应用获取PowerShell脚本输出
Get PowerShell script output from C++ App
这是使用应用程序读取PowerShell脚本输出C++更好方法。尝试使用以下代码,但无法获得输出。从控制台执行相同的PowerShell脚本是完全可以的,但希望获取PowerShell脚本的输出以在应用程序中使用相同的脚本。
system("start powershell.exe Set-ExecutionPolicy RemoteSigned n");
system("start powershell.exe d:\callPowerShell.ps1");
system("cls");
同样的问题也发生在我身上,以下是我的解决方法:将PowerShell的输出重定向到文本文件中,并在exe完成后,从文本文件中读取其输出。
std::string psfilename = "d:\test.ps1";
std::string resfilename = "d:\res.txt";
std::ofstream psfile;
psfile.open(psfilename);
//redirect the output of powershell into a text file
std::string powershell = "ls > " + resfilename + "n";
psfile << powershell << std::endl;
psfile.close();
system("start powershell.exe Set-ExecutionPolicy RemoteSigned n");
//"start": run in background
//system((std::string("start powershell.exe ") + psfilename).c_str());
system((std::string("powershell.exe ") + psfilename).c_str());
system("cls");
remove(psfilename.c_str());
//after the exe finished, read the result from that txt file
std::ifstream resfile(resfilename);
std::string line;
if (resfile.is_open()) {
std::cout << "result file opened" << std::endl;
while (getline(resfile, line)) {
std::cout << line << std::endl;
}
resfile.close();
remove(resfilename.c_str());
}
相关文章:
- 从C++应用获取PowerShell脚本输出
- 将.init_array部分包含在链接器脚本中会产生不可用的输出
- 如何从 bash 脚本捕获终端输出并将其显示在我的 Qt UI 中?
- 将Python脚本的输出链接到C 程序的输入
- 从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
- 在Qt应用程序中读取python脚本C++输出
- Shell脚本终止程序,但导致无法写入输出文件
- 如何将C++程序的输出用作 Bash 脚本中的变量
- 将C++程序输出与 Python 脚本链接
- CGI 渲染到屏幕外壳脚本输出
- 从C++执行 bash 脚本并读取其输出 ubuntu sdk
- 关闭标准输出和脚本
- 如何通过脚本将输出写入C++中的文本文件
- 将外部进程的输出管道传输到正在运行的脚本C++
- 使用C++或Bash/Shell脚本评估后台linux命令的输出
- 解释R脚本的valgrind输出
- 在QT中获取Python脚本的输出
- 从c++调用Python脚本并使用其输出
- 如何在c++中重定向读取bash脚本的输出
- 将python脚本输出重定向到grep