locked
catch return value from script in batch file RRS feed

  • Question

  • I'm running my PS scripts thru a Job scheduler that uses command files(cmd). How can I catch the return code from the script to either send it to next step or fail the process.


     
    PowerShell -file \\servera\powershell\cleanup_earned_hours_cm.ps1
    IF %ERRORLEVEL% EQU 0 GOTO loadit
       ECHO .
       ECHO Error during CSV File Creation
       ECHO RC = %ERRORLEVEL%
       ECHO .
       GOTO truncateerr
     Thanks.

    Friday, February 5, 2016 4:05 PM

Answers

  • Add the it your script file:

    Try{
          $ErrorActionPreference='Stop'
         # your script here
    }
    Catch{ exit 1 }


    \_(ツ)_/

    • Marked as answer by hart60 Friday, February 5, 2016 9:39 PM
    Friday, February 5, 2016 9:26 PM

All replies

  • The code you posted is cmd shell script (batch), not PowerShell.

    Question is far too vague. Remember that we don't have any information other than what you have posted.


    -- Bill Stewart [Bill_Stewart]

    Friday, February 5, 2016 4:52 PM
  • Question still too vague to be answered. Why, for instance, is there a need to mix PowerShell and a batch file? What is the purpose and what is provoking the question?

    hart60: Make sure that this is not an XY problem. Describe the actual problem, not how you think it needs to be solved.


    -- Bill Stewart [Bill_Stewart]

    Friday, February 5, 2016 5:18 PM
  •  I have to you use command file because the Job Scheduler we use can only call (.cmd) files.

     So I need to check if the step completes (return code 0) before going to next step in process.

    If PS fails then error the step and report back to Job Scheduler with a return code GT 0 so it
    fails the job and any subsequent jobs depending on this process don't start.


     Thanks.

     

    Friday, February 5, 2016 9:22 PM
  • I have to you use command file because the Job Scheduler we use can only call (.cmd) files.

    What "Job Scheduler" is that? The Windows Task Scheduler can run PowerShell scripts by calling powershell.exe -File scriptname.ps1. I do this all the time.

    .cmd files are not directly executable but are scripts run by cmd.exe.

    Your question is still too vague. You are leaving out too much information. Remember, we only know what you have posted.


    -- Bill Stewart [Bill_Stewart]

    Friday, February 5, 2016 9:25 PM
  • Add the it your script file:

    Try{
          $ErrorActionPreference='Stop'
         # your script here
    }
    Catch{ exit 1 }


    \_(ツ)_/

    • Marked as answer by hart60 Friday, February 5, 2016 9:39 PM
    Friday, February 5, 2016 9:26 PM
  • Unfortunately there are some old job schedulers that work with vendor packages.  They allow execution of external bathes and only accept zero or non-zero as an outcome.

    These older task managers usually have a configuration screen that allows you to enter internal commands.  The schedule command only accepts the name of a batch file.

    I cannot figure why companies still use this antiquated software but they do.

    The issue here, if my guess is correct, is that the user has a PS script and no PS knowledge hence no understanding of what we are saying about "exit".  I suspect there I also a language issue.  Wrapping the code in Try/Catch might work.


    \_(ツ)_/

    Friday, February 5, 2016 9:34 PM
  • I applaud the guesswork.

    The OP's question is nearly impossibly vague and, at best, very incomplete.

    There is still not nearly enough information in the question to give any kind of useful answer.


    -- Bill Stewart [Bill_Stewart]

    Friday, February 5, 2016 9:38 PM
  • Looks like you guessed right...

    -- Bill Stewart [Bill_Stewart]

    Friday, February 5, 2016 9:46 PM
  • Looks like you guessed right...

    -- Bill Stewart [Bill_Stewart]


    Sometimes I get lucky.  I agree.  It was hard to decode but I have seen this before.

    \_(ツ)_/

    Friday, February 5, 2016 10:18 PM