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?

7
  • 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 ;-) Commented Sep 16, 2021 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. Commented Sep 16, 2021 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 :) Commented Sep 16, 2021 at 13:48
  • Try JosefZ recommendation! ;-) Commented Sep 16, 2021 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. ;-) Commented Sep 16, 2021 at 13:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OMG ;-) ... really? ;-)
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.