Safest way to run BAT file from Powershell script
我无法通过Powershell脚本直接执行bat文件。 例如,这在命令行上有效:
1 | .\\my-app\my-fle.bat |
当我将此命令添加到脚本时,它输出:
1 2 3 4 | 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. |
我也尝试了以下操作,结果相同:
1 2 3 4 5 | & .\\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,因为我需要验证批处理是否成功。
1 | cmd.exe /c '\my-app\my-file.bat' |
要运行.bat并有权访问最后一个退出代码,请按以下方式运行它:
1 | & .\my-app\my-fle.bat |
尝试此操作,您的点源稍微有些偏离。 编辑,为OP添加lastexitcode位。
1 | $A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode |
为不可见的批处理添加