如何暂停足够长的时间以使 cmd.exe 退出?
How to pause long enough that cmd.exe exits?
我正在使用命令行文件 (MyTestRepo.cmd) 中的以下代码(有效)更新 TortoiseGit 存储库:
cd c:\MyTortoiseGitRepo
git.exe pull --progress -v --no-rebase "origin"
在 PowerShell 中,我使用以下代码调用此文件:
$TestPull = Start-Job { Invoke-Item C:\MyTests\MyTestRepo.cmd }
Wait-Job $TestPull
Receive-Job $TestPull
上面的代码确实有效,但它等待 CMD 文件完成 运行 并退出 cmd.exe 的时间不够长,以便在继续下一行代码之前退出。
对于必须等待 cmd.exe 进程完成才能继续进行的更好的方法是什么?
Invoke-Item
不支持等待。您可以使用调用运算符 &
。例如:
$TestPull = Start-Job { & "C:\MyTests\MyTestRepo.cmd" }
或Start-Process -Wait
:
$TestPull = Start-Job { Start-Process -FilePath "C:\MyTests\MyTestRepo.cmd" -Wait }
Start-Process
将在用户执行脚本时显示 cmd-window。这可以通过添加 -
NoNewWindow` 来抑制。
我正在使用命令行文件 (MyTestRepo.cmd) 中的以下代码(有效)更新 TortoiseGit 存储库:
cd c:\MyTortoiseGitRepo
git.exe pull --progress -v --no-rebase "origin"
在 PowerShell 中,我使用以下代码调用此文件:
$TestPull = Start-Job { Invoke-Item C:\MyTests\MyTestRepo.cmd }
Wait-Job $TestPull
Receive-Job $TestPull
上面的代码确实有效,但它等待 CMD 文件完成 运行 并退出 cmd.exe 的时间不够长,以便在继续下一行代码之前退出。
对于必须等待 cmd.exe 进程完成才能继续进行的更好的方法是什么?
Invoke-Item
不支持等待。您可以使用调用运算符 &
。例如:
$TestPull = Start-Job { & "C:\MyTests\MyTestRepo.cmd" }
或Start-Process -Wait
:
$TestPull = Start-Job { Start-Process -FilePath "C:\MyTests\MyTestRepo.cmd" -Wait }
Start-Process
将在用户执行脚本时显示 cmd-window。这可以通过添加 -
NoNewWindow` 来抑制。