Scott Bass
2012-02-22 04:05:25 UTC
(Hoping to get an answer here...)
Summary:
See http://social.technet.microsoft.com/Forums/en-AU/winserverpowershell/thread/7135b7bb-ef90-4c17-8170-ff3aaae8cfa9.
Details:
Try this:
1. Create a .bat or .cmd file that returns a non-zero return code. I
called mine error.cmd.
@exit 123
2. Create the below script. $cmd is the path to where you created
error.cmd:
# specify path to dummy batch file that returns non-zero exit code
$cmd="$env:userprofile\My Documents\My Powershell Scripts\error.cmd"
# invoke using cmd / c - this works
cmd /c " ""$cmd"" "
"LastExitCode=$LastExitCode"
# "reset" LastExitCode
$LastExitCode=999
# invoke using Start-Process - this does not work
Start-Process $cmd -Wait
"LastExitCode=$LastExitCode"
Execute this script. When I invoke error.cmd via cmd /c, it works,
i.e. $LastExitCode has the correct value. When I invoke error.cmd via
Start-Process, $LastExitCode does not have the correct value.
I thought the whole idea of PowerShell was to replace the ancient and
klunky cmd.exe. (Ok, it's not the "whole idea", but hopefully you get
what I mean without arguing semantics).
Sooooo...can I:
1) Invoke an external command without using cmd.exe (via Start-
Process, Invoke-Command, Invoke-Expression, whatever)
2) Pause the script until that command finishes
3) Have the correct value of $LastExitCode based on the return code
(%ERRORLEVEL%) from the invoked external command?
Thanks,
Scott
Summary:
See http://social.technet.microsoft.com/Forums/en-AU/winserverpowershell/thread/7135b7bb-ef90-4c17-8170-ff3aaae8cfa9.
Details:
Try this:
1. Create a .bat or .cmd file that returns a non-zero return code. I
called mine error.cmd.
@exit 123
2. Create the below script. $cmd is the path to where you created
error.cmd:
# specify path to dummy batch file that returns non-zero exit code
$cmd="$env:userprofile\My Documents\My Powershell Scripts\error.cmd"
# invoke using cmd / c - this works
cmd /c " ""$cmd"" "
"LastExitCode=$LastExitCode"
# "reset" LastExitCode
$LastExitCode=999
# invoke using Start-Process - this does not work
Start-Process $cmd -Wait
"LastExitCode=$LastExitCode"
Execute this script. When I invoke error.cmd via cmd /c, it works,
i.e. $LastExitCode has the correct value. When I invoke error.cmd via
Start-Process, $LastExitCode does not have the correct value.
I thought the whole idea of PowerShell was to replace the ancient and
klunky cmd.exe. (Ok, it's not the "whole idea", but hopefully you get
what I mean without arguing semantics).
Sooooo...can I:
1) Invoke an external command without using cmd.exe (via Start-
Process, Invoke-Command, Invoke-Expression, whatever)
2) Pause the script until that command finishes
3) Have the correct value of $LastExitCode based on the return code
(%ERRORLEVEL%) from the invoked external command?
Thanks,
Scott