首页 > 解决方案 > 在启动powershell后保持批处理脚本运行

问题描述

我正在使用批处理脚本来自动化我必须在计算机上运行的许多任务。在批处理文件中,我有一个菜单让您分别在每个任务之间进行选择或执行所有操作。当我选择做所有事情时,在启动第一个 .ps1 后,它只会杀死我的 cmd 窗口。

我找到了一些方法来保持窗口打开,但脚本没有继续......我还创建了一个“主文件”来调用所有“.ps1”并尝试了这些 cmds:call、/wait 调用功能有效并且它们一切开始。所以我不明白为什么他们会在我的脚本中做同样的事情。在我的脚本中,下面的 cmd 行不会互相吼叫,因为我希望能够单独调用它们。

我想正确运行的部分脚本:

echo Windows Layout
start /wait PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Customisation\Export_Import_Layout.ps1""' -Verb RunAs}"

echo Remove Unwanted Apps
start /wait PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Clean\RemoveAppx.ps1""' -Verb RunAs}"
wmic product where name="WinZip 22.5" call uninstall
ping -n 6 localhost >nul
Taskkill /IM /F "MicrosoftEdge.exe"
Taskkill /IM /F "MicrosoftEdgeCP.exe"
Taskkill /IM /F "MicrosoftEdgeSH.exe"
%~dp0Clean\MCPR.exe

master.bat 看起来像这样:

call PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0InstallChocoAndApps.ps1""' -Verb RunAs}"
call PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Clean\RemoveAppx.ps1""' -Verb RunAs}"

任何提示都会被应用。请记住,我不是脚本专家。

谢谢

标签: windowspowershellbatch-filesequential

解决方案


设法通过添加 start "" /wait 而不是 start /wait 来解决我的问题


推荐阅读