Batch file 从Powershell脚本运行BAT文件的最安全方法
我无法获取直接执行bat文件的powershell脚本。例如,这在命令行上起作用:Batch file 从Powershell脚本运行BAT文件的最安全方法,batch-file,powershell,Batch File,Powershell,我无法获取直接执行bat文件的powershell脚本。例如,这在命令行上起作用: .\\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 wa
.\\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,因为我需要验证批处理是否成功。试试这个,您的点源有点不正确。编辑,为OP添加lastexitcode位
$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode
为不可见批处理添加
-WindowStyle Hidden
。要运行.bat并访问最后一个退出代码,请按以下方式运行它:
& .\my-app\my-fle.bat
@瑞南的解决方案对我有效。不过,我还有一些额外的要求:
& runner.bat bat_to_run.bat logfile.txt
[奔跑者,蝙蝠]
@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
假设我的应用程序是当前目录下的子目录。$LASTEXITCODE应该在最后一个命令中:
.\my-app\my-fle.bat
如果它来自文件共享:
\\server\my-file.bat
那有效的
调用项目脚本.bat
呢。这也会返回LastExitCode中的退出状态吗?要从开始进程访问$LastExitCode,您需要将该命令辅助到一个变量,并使用passthru开关,然后从变量读取exitcode。这仅在my app
位于相对路径或我使用驱动器号时有效。我试图避免将脚本绑定到特定驱动器。@Casey我想我误解了。那么我的应用程序
是你正在使用的任何驱动器的根目录下的文件夹吗?如果是这样的话,&\my app\my file.bat
对我很有效。如何调用.ps1脚本?两个反斜杠表示服务器共享。您所说的最安全是什么意思?我如何在这里向bat文件添加参数?特别是如果参数是变量?我尝试了这个[Start Process“cmd.exe”“/B/C test.bat$inputFilePath
”],但它说:“找不到接受参数“test.bat”的位置参数。”这并没有提供问题的答案。一旦你有足够的钱,你将能够;相反-
\\server\my-file.bat