首页 > 解决方案 > 无法使用 `call` 或 `start` 使用 BATCH 启动 ps1 文件

问题描述

我遇到了一个批处理命令的问题,它不会启动我要做的任何事情,即使使用call不会启动它。

:choice2
set /P c1="text here [Y/N]? "
if /I "%c1%" EQU "Y" start "" /wait /high /max "Powershell.exe -executionpolicy remotesigned -File %batdir%batch\filename.ps1"
if /I "%c1%" EQU "N" goto :end
goto :choice2

主要要求是 %batdir% 在第一if行,因为批处理从闪存驱动器运行。

没有尝试过%batdir%,无论哪种方式都行不通。尝试对powershell.exe使用不同的命令,尝试使用直接dir的powershell,不起作用,总是得到“找不到文件,确保它输入正确。”。

我将不胜感激任何人都可以提供的帮助。

标签: windowspowershellbatch-file

解决方案


由于您没有提供Minimal Complete and Verifiable Example,因此假定这%batdir%是正在运行的批处理文件的位置,我已将其替换为适当的%~dp0变量。您还应该注意,我还替换了您的set /p命令,因为这不是用于已知单键输入的正确命令:

:choice2
"%SystemRoot%\System32\choice.exe" /M "text here"
If ErrorLevel 2 GoTo end
Start "" /Wait /High /Max "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy RemoteSigned -File "%~dp0batch\filename.ps1"
GoTo choice2

:end
Pause

我不推荐使用/Highor/Max来完成这个任务,(包括他们只是因为你做了)。因此,因为您说您也尝试过call,所以在我看来,您可能也不需要使用start这些选项。如果只需要运行powershell脚本并等待它完成,在返回输入提示之前,试试这个修改:

:choice2
"%SystemRoot%\System32\choice.exe" /M "text here"
If ErrorLevel 2 GoTo end
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy RemoteSigned -File "%~dp0batch\filename.ps1"
GoTo choice2

:end
Pause

推荐阅读