0

I have a power shell script that invokes a .cmd file. Here the architecture looks like

-PowerShell File Code

$arguments='/savecred /profile /user:myDomain\myuser "cmd /c C:\Users\myuser\code.cmd"' Start-Process cmd.exe $arguments -Wait

  • .cmd code file

Here the code invokes wget request to download a file

wget .....(command here)

My goal is to learn at PowerShell command prompt (after completing Start-Process command) whether the wget command executed successfully or some error like 401, 404 occurred during the execution. Here I am particular not interested in the type of error, just need to know whether error occurred or not.

2 Answers 2

0

Not sure if this is what you are asking but you could test none-zero return code of the last command using $? variable, which will be $false if return code is not zero.

Let's say you have a test.cmd file which simply returns 5:

exit 5

If you run it within PowerShell, you can look at the result of $?

if ($?) {"No error"} else {"some error"}

Here is a link for more info: http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/12/powershell-error-handling-and-why-you-should-care.aspx

0

Using $? with Start-Process isn't going to work:

C:\PS> Start-Process cmd.exe -arg '/c exit 5'
C:\PS> $?
True

If you want to use Start-Process, you can go this route:

C:\PS> $p = Start-Process cmd.exe -arg '/c exit 5' -PassThru -Wait
C:\PS> $p.ExitCode
5

Or you could just invoke cmd.exe directly:

C:\PS> cmd /c exit 5
C:\PS> $LASTEXITCODE
5

In this last example you could use $? but I prefer $LastExitCode since some brain-damaged console apps return non-zero for success. Regarding calling cmd.exe and using $LASTEXITCODE see this ScriptingGuy blog post.

For a handy CheckLastExitCode function look through this blog post for an implementation of the function.

4
  • Yes, that's true for start-process but both $? and $lastexitcode works with cmd /c exit 5 Commented Oct 7, 2013 at 23:52
  • I am trying to use $? and it returns true, even though my wget command in the code.cmd file fails. Is there a way to return non-zero exitcode to PowerShell, if wget command fails. Commented Oct 8, 2013 at 0:03
  • @VarunGupta Yes there is. If you want to continue to use Start-Procss, use the middle example above that captures the process object in $p and then uses its ExitCode property. If you eliminate the Start-Process and call cmd.exe directly, use the last example above which uses $LastExitCode.
    – Keith Hill
    Commented Oct 8, 2013 at 1:58
  • @AdilH. Whether or not that "works" depends on your semantics for a return value of 5. If that is meant to indicate failure i.e. $? should be false then yeah it works. However I've seen EXEs where 5 would be a success value so $? returning false would not work.
    – Keith Hill
    Commented Oct 8, 2013 at 1:59

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.