Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a .bat file from psake?

I am trying in a powershell psake script to execute a .bat file. Is this possible? Or do I have to do a workaround?

like image 208
Jan-Terje Sørensen Avatar asked Dec 03 '25 19:12

Jan-Terje Sørensen


2 Answers

Try the following:

task CallBatch {
  exec {cmd.exe /c "path\to\my\testscript.bat"}
}

It is not necessary to wrap the call to cmd.exe in PSake's exec {} function, but if you do it, the build fails if the batch returns anything but 0.

The task below always lets the build fail:

task Return1FromCmd {
  exec {cmd.exe /c "@exit 1"}
}
like image 148
MZywitza Avatar answered Dec 06 '25 04:12

MZywitza


To execute a .bat (or .cmd) from PowerShell:

foo.bat:

@echo off
echo "foo"

foo.ps1:

. .\foo.bat
#or
.\foo.bat
#or
& .\foo.bat

we can then run the script:

D:\dev> .\foo.ps1
"foo"
like image 29
Ian Davis Avatar answered Dec 06 '25 05:12

Ian Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!