我无法获得Powershell脚本来直接执行bat文件。例如,这在命令行上有效:
.\\my-app\my-fle.bat
当我将此命令添加到脚本时,它输出:
The term '.\\my-app\my-file.bat' is not recognized as the
name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
我也尝试了以下操作,结果相同:
& .\\my-app\my-fle.bat
& ".\\my-app\my-fle.bat"
\my-app\my-fle.bat
& \my-app\my-fle.bat
& "\my-app\my-fle.bat"
注意:它必须返回lastexitcode,因为我需要验证批处理是否成功。
cmd.exe /c '\my-app\my-file.bat'
& .\my-app\my-fle.bat
$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode
为不可见的批处理添加-WindowStyle Hidden
。
[[PS脚本代码]
& runner.bat bat_to_run.bat logfile.txt
[runner.bat]
@echo OFF REM This script can be executed from within a powershell script so that the bat file REM passed as %1 will not cause execution to halt if PAUSE is encountered. REM If {logfile} is included, bat file output will be appended to logfile. REM REM Usage: REM runner.bat [path of bat script to execute] {logfile} if not [%2] == [] GOTO APPEND_OUTPUT @echo | call %1 GOTO EXIT :APPEND_OUTPUT @echo | call %1 1> %2 2>&1 :EXIT
.\my-app\my-fle.bat