从Powershell脚本运行BAT文件的最安全方法

时间:2021-10-21 05:32:28

I can not get a powershell script to execute a bat file directly. For example, this works on the command line:

我无法获得powershell脚本来直接执行bat文件。例如,这适用于命令行:

.\\my-app\my-fle.bat

When I add this command to a script, it outputs:

当我将此命令添加到脚本时,它输出:

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.

I also tried the following, with the same result:

我也试过以下,结果相同:

& .\\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"

Note: It must return the lastexitcode as I need to verify success of batch.

注意:必须返回lastexitcode,因为我需要验证批处理是否成功。

3 个解决方案

#1


63  

cmd.exe /c '\my-app\my-file.bat'

#2


21  

To run the .bat, and have access to the last exit code, run it as:

要运行.bat,并且可以访问最后一个退出代码,请将其运行为:

 & .\my-app\my-fle.bat

#3


15  

Try this, your dot source was a little off. Edit, adding lastexitcode bits for OP.

试试这个,你的点源有点偏。编辑,为OP添加lastexitcode位。

$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode

$ A = Start-Process -FilePath。\ my-app \ my-fle.bat -Wait -passthru; $ a.ExitCode

add -WindowStyle Hidden for invisible batch.

add -WindowStyle隐藏为隐藏批处理。

#1


63  

cmd.exe /c '\my-app\my-file.bat'

#2


21  

To run the .bat, and have access to the last exit code, run it as:

要运行.bat,并且可以访问最后一个退出代码,请将其运行为:

 & .\my-app\my-fle.bat

#3


15  

Try this, your dot source was a little off. Edit, adding lastexitcode bits for OP.

试试这个,你的点源有点偏。编辑,为OP添加lastexitcode位。

$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode

$ A = Start-Process -FilePath。\ my-app \ my-fle.bat -Wait -passthru; $ a.ExitCode

add -WindowStyle Hidden for invisible batch.

add -WindowStyle隐藏为隐藏批处理。