C ++创建进程PowerShell作为管理员,隐藏,不要等待它

c++ createprocess powershell as admin, hidden and dont wait for it

本文关键字:隐藏 等待 管理员 创建 进程 PowerShell      更新时间:2023-10-16

这就是我所拥有的,在没有命令的情况下启动powershell.exe并在它之后直接关闭。为什么它不起作用?

int main(int argc, char *argv[])
{
[...]
CreateProcess( NULL,   // No module name (use command line)
    "powershell.exe -command ".C:\test\t.ps1"   ",      
[...]
        &si,            // Pointer to STARTUPINFO structure
        &pi );          // Pointer to PROCESS_INFORMATION structure
return 0;
}

在普通cmd中,命令如下所示:

powershell -command ".c:testt.ps1"

在文件中,如果要测试它,请

在此单行中:
write-host "hello world" |out-file C:testhi.txt

应该在控制台中写 hello world 并在文件夹中创建 hi.txt

命令行应为:

CreateProcess(NULL, // No module name (use command line)
    "powershell.exe -command "& {C:\test\t.ps1}"",  

CreateProcess(NULL, // No module name (use command line)
    "powershell.exe -file C:\test\t.ps1",  

通常,对于执行脚本,请使用 -File,除非退出代码对您很重要。 如果是,请使用 -Command,因为 -File 存在一个错误,即使存在错误,它也始终返回 0(成功)。

如果希望执行 powershell.exe 提示提升,请改用 ShellExecute API。 传入"RunAs" for lpOperation,您可以使用 nShowCmd 参数指定隐藏窗口。