I am trying in a powershell psake script to execute a .bat file. Is this possible? Or do I have to do a workaround?
Asked
Active
Viewed 487 times
3 Answers
3
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"}
}

MZywitza
- 3,121
- 1
- 17
- 12
1
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"

Ian Davis
- 3,848
- 1
- 24
- 30
1
This works for me:
properties {
$mybat = 'C:\path\tool.bat'
}
task Test -depends ... {
"Bla bla"
Exec { & $mybat }
}
No need to directly mention cmd.exe
-- using &
in the Exec script block seems to be enough.

Martin Ba
- 37,187
- 33
- 183
- 337