1

I need to run the following batch file:

cd ..
msbuild reve_host.vcxproj

In order for msbuild to work, it needs to be run through a specific shell. Its path is stored in $executorPath in my Powershell script, which looks like this:

$executorPath = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2019'
Write-Host "STAGE: CLEAN."

Here are several ways of executing that I have tried, that have not worked:

  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')

With 1 and 2, I get:

Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

With 3 and 4, I get:

The term 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2019' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

None of these work. How do I run this?

martorad
  • 165
  • 1
  • 12
  • First of all - you have to concatenate the path and file name with a backslash to make it a full path. And second - a `*.bat` is not an executable - `CMD.exe` is. And bat should be provided as the second argument after `/c` if you want to run it. Otherwise you would need to use `Invoke-Expression` ;-) – Olaf Sep 16 '21 at 13:43
  • 1
    Try `. (Join-Path -Path $executorPath -ChildPath BUILD.bat)` or (another approach) `& "$(Join-Path -Path $executorPath -ChildPath BUILD.bat)"` or something alike. – JosefZ Sep 16 '21 at 13:47
  • @Olaf I'm not sure I understand what you mean by concatenate the path and file name. Do you think you could make a full answer explaining you solution more thoroughly? I'm quite new to this :) – martorad Sep 16 '21 at 13:48
  • Try JosefZ recommendation! ;-) – Olaf Sep 16 '21 at 13:50
  • What i mean is `"C:\randomPath\withSubfolder" "fileName.bat"` is not the same like `"C:\randomPath\withSubfolder\fileName.bat"`. Only second one is a valid full path. ;-) – Olaf Sep 16 '21 at 13:52
  • @JosefZ With both of your suggestions, I get the same error as is listed in my original post, in "With 3 and 4, I get:" – martorad Sep 16 '21 at 13:53
  • This might help ... [Call a batch file from PowerShell....](https://stackoverflow.com/a/66977017/9196560) .. take a look to the answer. You should `cd` into the folder or provide the complete path of the bat file. – Olaf Sep 16 '21 at 14:00

2 Answers2

1

I found an alternative solution. Using this module allows MsBuild to be called directly from Powershell and saves the hassle of manually calling the correct shell. This circumvents the problem, instead of solving it though.

martorad
  • 165
  • 1
  • 12
0

Or Simply:

#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:\BEKDocs\Batch\batch1.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:\BEKDocs\Batch\batch1.cmd"

PS> & $ExecutionPath
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there
 Hello there

PS> 

EDIT: It also works if the extension is .bat!

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21