Getting the exit code of batch script that is called within a powershell script

Welcome Forums General PowerShell Q&A Getting the exit code of batch script that is called within a powershell script

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #84661

    Participant
    Points: 0
    Rank: Member

    Hello,

    I'm trying to capture and display the exit code of a batch (.bat) script that is called within a powershell (.ps1) script. I'm new to power shell scripting. Greatly appreciate if you can provide any inputs or sample code on how this could be achieved.

    Below is the .bat script that is being called with various parameters from the .ps1 script. How do I capture and display the exit code of this istool.bat script?

     .\istool.bat glossary export -dom serverhostname:9446 -u user -p password -filename "c:\exp_polrules.xml"  -allpolrul 

    Many thanks!
    Naveen.

  • #84662

    Participant
    Points: 264
    Helping Hand
    Rank: Contributor
  • #84667

    Participant
    Points: 322
    Helping Hand
    Rank: Contributor

    You don't show how you are capturing the error in the batch file or even how you are exiting the batch file.
    Anyway, in most cases, $LASTEXITCODE (which is the equivalent to %ERRORLEVEL%) is what you'd normally use to retrieve this data point.

    So, something like..
    & cmd.exe /c
    $LASTEXITCODE

    cmd /c dir x:\
    The system cannot find the path specified.
    ...
    $lastexitcode
    1

    cmd /c dir c:\
    Volume in drive C is Root
    Volume Serial Number is 7E63-D40D

    Directory of c:\
    ...
    $lastexitcode
    0

    See details here:
    'blogs.msdn.microsoft.com/powershell/2007/05/21/cmd-exe-compatibility'
    'blogs.technet.microsoft.com/heyscriptingguy/2011/06/06/get-legacy-exit-codes-in-powershell'

The topic ‘Getting the exit code of batch script that is called within a powershell script’ is closed to new replies.