如何从Powershell调用批处理文件,使用自定义的.cmd来运行它?



我需要运行以下批处理文件:

cd ..
msbuild reve_host.vcxproj

为了使msbuild工作,它需要通过一个特定的shell运行。它的路径存储在Powershell脚本的$executorPath中,如下所示:

$executorPath = 'C:ProgramDataMicrosoftWindowsStart MenuProgramsVisual Studio 2019Visual Studio ToolsVCx64 Native Tools Command Prompt for VS 2019'
Write-Host "STAGE: CLEAN."

下面是我尝试过的几种执行方法,但都没有成功:

  1. Invoke-Command '$executorPath "BUILD.bat"'
  2. Invoke-Command '$executorPath ".BUILD.bat"'
  3. Invoke-Command -ScriptBlock {& $executorPath 'BUILD.bat'}
  4. Invoke-Command -ScriptBlock {& $executorPath '.BUILD.bat'}
  5. $($executorPath 'BUILD.bat')
  6. $($executorPath '.BUILD.bat')

有了1和2,我得到:

参数集无法使用指定的命名参数解析。不能同时使用一个或多个发出的参数,或者提供的参数数量不足。

有3和4,我得到:

术语"C:ProgramDataMicrosoftWindowsStart MenuProgramsVisual Studio 2019Visual Studio ToolsVCx64 Native Tools Command Prompt for VS 2019"不被识别为cmdlet、函数、脚本文件或可执行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后再试一次。

这些都不起作用。我怎么运行这个?

我找到了另一个解决方案。使用这个模块可以直接从Powershell调用MsBuild,从而省去了手动调用正确shell的麻烦。这是在回避问题,而不是解决问题。

或者简写为:

#Test cmd file:
@Echo off
for %%i in (1,2,3,4,5,6,7,8,9,10) DO Echo. Hello there
exit /B 200
PS> & G:BEKDocsBatchbatch1.cmd
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
#To Use your example code:
PS> $ExecutionPath = "G:BEKDocsBatchbatch1.cmd"
PS> & $ExecutionPath
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
Hello there
PS> 

编辑:它也工作,如果扩展名是。bat!