等待 PsExec 在 PowerShell 中调用 cmd/c 完成 [英] Wait for PsExec calling cmd /c to finish in PowerShell
问题描述
我有一个动态生成的批处理文件,我将它推送到远程 PC,然后使用 PsExec 执行它.我面临的问题是,一旦调用该行,PowerShell 脚本就会继续前进,并且不会等待它完成.这是我所拥有的:
I have a dynamically generated batch file that I push out to a remote PC and then execute it using PsExec. The issue I am facing is that as soon as that line is called, the PowerShell script moves on and doesn't wait for it to finish. Here is what I have:
psexec -accepteula \\$Server -u Username -p Password-d -i 2 cmd /c C:\Call.bat
Call.bat
使用传入的几个参数调用远程机器上的可执行文件.这个文件是动态生成的,每次都不同,但看起来像这样:
Call.bat
calls an executable on the remote machine with a few parameters passed in. This file is dynamically generated and is different every time, but can look like this:
cd C:\20161212-175524
C:\20161212-175524\RE.exe /pa1 /pa2 /pa3 /pa4 /pa5 /pa6 /pa7 /pa8 /pa9 /pa10
批处理文件需要作为特定用户的交互式脚本运行,但我需要它至少等待生成的进程完成.我曾尝试添加 2>&1
和 |Out-Null
The batch file needs to run as an interactive script as that specific user, but I need it to at least wait for the spawned process to finish. I have tried adding 2>&1
and | Out-Null
理想情况下,我想检索生成的进程返回的退出代码,但这可能太多了.
Ideally, I would like to retrieve the exit code returned by the spawned process, but that may be too much.
推荐答案
以前我用过这样的东西来实现你所追求的等待:
Previously i have used something like this to achieve the waiting you are after:
Start-Process -FilePath 'c:\tools\PSexec.exe' -ArgumentList "-u MyUserName -p $password \\$Computer .\Run.bat $Var >> C:\Temp\$Computer.log" -Wait -Passthru -WindowStyle Hidden
您需要关注上一行的是通过使用 Start-Process
cmdlet,我们可以使用 -Wait
参数.
What you need to focus on the line above is by using the Start-Process
cmdlet we can use the -Wait
parameter.
希望这能回答您的问题
这篇关于等待 PsExec 在 PowerShell 中调用 cmd/c 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!