c++ - 使用 Qt 运行 Windows PowerShell 命令
问题描述
在这篇文章之后,我打算在具有管理员权限的 Windows 10 上的 Qt 5.12.6 应用程序中运行此命令:
powershell -Command "agent.exe -Verb runAs"
我agent.exe
的旁边是我的 Qt 应用程序可执行文件,即它的父目录是QCoreApplication::applicationDirPath()
.
受另一篇文章的启发,我的 C++/Qt 代码如下所示:
m_agent = new QProcess(this);
QString agentName = "/agent.exe";
// "agent.exe" executable is next to application executable
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
// I'm not sure how to compose `args`
args << "-Command"; // ?
args << agentPath; // ?
args << "-Verb"; // ?
args << "runAs"; // ?
m_agent->start("powershell ", args);
我的当前args
,上面组成,没有启动agent.exe
.
我的问题是:我应该如何args
编写才能使用 Qt 运行我的 Windows PowerShell 命令?
解决方案
一些观察有助于解决问题。
现在agent.exe
使用以下代码以管理员身份运行:
QString agentName = "/agent.exe";
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
args << "-Command";
args << "Start-Process";
args << agentPath;
args << "-Verb";
args << "runAs";
m_agent->start("powershell", args);
更新
或者,您可以按照@Botje 的建议执行此操作
QString agentName = "/agent.exe";
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
args = QStringList({"-Command", QString("Start-Process %1 -Verb runAs").arg(agentPath)});
m_agent->start("powershell", args);
推荐阅读
- javascript - 如何实时检查多个输入的值?
- powerbi - 在用户加载 PowerBI 站点之前,嵌入 URL 不会出现在 IFRAME 中
- c# - 重新加载文件并保存最后一行位置
- python - Python:将JPG图像添加到folium中的弹出窗口
- g++ - QEMU可以用g++编译吗?
- react-native - 使用 Expo 计步器时无法通过步骤
- windows-subsystem-for-linux - WSL 中的 Geckodriver 卡在“交互式”的 readyState 中
- python - 使用 WSL 中的 numpy 在安装 MKL 时引发 RuntimeError(f'Cannot parse version {FULLVERSION}')
- cloud-foundry - KubeCF api 不适用于本地 Kind Cluster
- html - Bootstrap.: col-sm 行为很早。i,e 当尺寸小于 767px 时它变成 col-sm