Script Center > Scripting Forums > The Official Scripting Guys Forum! > powershell v1 - return error code to master script not working properly
Ask a questionAsk a question
 

Answerpowershell v1 - return error code to master script not working properly

  • Wednesday, November 04, 2009 4:40 AMNicoinThailand Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi.

    I got a master script calling a subscript.
    The subscript calls dtexec to execute a dts and then logs the result in a database. (sort of step level logging)

    Since the error is handled, I need it to "fail" in order for the master script to log it's end result as well (that will be a batch level logging)

    So what I do is this:

    MASTER:
    D:\Agoda\Application\powershell\scripts\Ps_Booking_Prestaging.ps1 $v_batchid $v_jobid $v_conn $v_to $v_from
    if ($LASTEXITCODE -ne 0)
    {
    log-batchend $v_batchid "FAILED" $v_conn
    exit
    }

    SUBSCRIPT:
    #run task
    $v_logid = log-taskstart $v_batchid $v_jobid $v_taskid $v_name $v_conn $v_to $v_from
    $results = rundtsx $v_dtsxFile 0 $v_batchid $v_jobid $v_taskid $v_conn $v_to $v_from
    if ($results -ne 0)
    {
      log-taskend $v_logid $v_batchid $v_jobid $v_taskid  "FAILED" "909000002" $v_conn
      throw "a runtime error has occured"
      exit
    }

    I use throw to throw and error if it fails which should then be catched by MASTER.
    I do have the throw error and the log table is properly updated with a "FAILED" status but at MASTER level I get nothing.

    I've tried using if !($?) instead to no avail. It looks like throw command stops the sub script and the master script right there.

    Anybody has an idea on how to handle that?

    Thanks

    Nico

Answers

  • Wednesday, November 04, 2009 4:12 PMAbqBillModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    PowerShell's exit statement supports an exit code; e.g. 'exit 2' exits a script and sets the script's exit code to 2.

    Bill

All Replies

  • Wednesday, November 04, 2009 4:12 PMAbqBillModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    PowerShell's exit statement supports an exit code; e.g. 'exit 2' exits a script and sets the script's exit code to 2.

    Bill
  • Thursday, November 05, 2009 2:11 AMNicoinThailand Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi BIll.

    This is awesome. Thanks for the short but very helpful answer!

    Going to try that right away. Will mark your reply as answer after test :)

    Nico