I can not get a powershell script to execute a bat file directly. For example, this works on the command line:
.\\my-app\my-fle.bat
When I add this command to a script, it outputs:
The term '.\\my-app\my-file.bat' is not recognized as the
name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
I also tried the following, with the same result:
& .\\my-app\my-fle.bat
& ".\\my-app\my-fle.bat"
\my-app\my-fle.bat
& \my-app\my-fle.bat
& "\my-app\my-fle.bat"
Note: It must return the lastexitcode as I need to verify success of batch.
This question is related to
batch-file
powershell
try running after changing file name from '-' to `_'
for eg:
.\my_app\my_fle.bat
instead of
.\\my-app\my-fle.bat
Or
cd my_app
.\my_file.bat
What about invoke-item script.bat
.
To run the .bat, and have access to the last exit code, run it as:
& .\my-app\my-fle.bat
cmd.exe /c '\my-app\my-file.bat'
Assuming my-app is a subdirectory under the current directory. The $LASTEXITCODE should be there from the last command:
.\my-app\my-fle.bat
If it was from a fileshare:
\\server\my-file.bat
@Rynant 's solution worked for me. I had a couple of additional requirements though:
Here's what I got working (finally):
[PS script code]
& runner.bat bat_to_run.bat logfile.txt
[runner.bat]
@echo OFF
REM This script can be executed from within a powershell script so that the bat file
REM passed as %1 will not cause execution to halt if PAUSE is encountered.
REM If {logfile} is included, bat file output will be appended to logfile.
REM
REM Usage:
REM runner.bat [path of bat script to execute] {logfile}
if not [%2] == [] GOTO APPEND_OUTPUT
@echo | call %1
GOTO EXIT
:APPEND_OUTPUT
@echo | call %1 1> %2 2>&1
:EXIT
Try this, your dot source was a little off. Edit, adding lastexitcode bits for OP.
$A = Start-Process -FilePath .\my-app\my-fle.bat -Wait -passthru;$a.ExitCode
add -WindowStyle Hidden
for invisible batch.
Source: Stackoverflow.com