Automic Workload Automation

 View Only
Expand all | Collapse all

Job return code is always zero when powershell script ha syntax error

  • 1.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 07, 2017 09:23 AM
    Hi,
    I came across a weird issue. It looks like that for Win job when the interpreter is powershell if the script has any syntax error the job will return successfully anyway, i.e. return code is 0.
    My guess is that this a powershell issue. Anybody knows how to solve it?

    Best,
    Marco


  • 2.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 07, 2017 01:40 PM
    I am not very familiar with powershell (still learning)

    I do an exit $PS_exception if any error might occur.

    so if a function does return an error I do an exit with a defined exit code - see this stupid example:

    :SET &WEEKDAY# = "friday"

    :begin_ext_int POWERSHELL

    :SET &WEEKDAY# = "friday"

    :begin_ext_int POWERSHELL

    $weekday= '&WEEKDAY#'
    $output = ''

    if ($weekday -eq 'monday') {
            write-host 'monday'
            $output= 'MO'
            $PS_exception = 11
        } elseif ($weekday -eq 'tuesday') {
            write-host 'tuesday';$output= 'TU';$PS_exception = 12
        } elseif ($weekday -eq 'wednesday') {
            write-host 'wednesday';$output= 'WE';$PS_exception = 13
        } elseif ($weekday -eq 'thursday') {
            write-host 'thursday';$output= 'TH';$PS_exception = 14
        } elseif ($weekday -eq 'friday') {
            write-host 'friday';$output= 'FR';$PS_exception = 15
        } elseif ($weekday -eq 'saturday') {
            write-host 'saturday';$output= 'SA'; $PS_exception = 0
        } elseif ($weekday -eq 'sonday') {
            write-host 'sunday';$output= 'SU'; $PS_exception = 0
        }

    write-host "Output: " $output
    write-host "Function RC: "$PS_exception
    exit $PS_exception
    :END_EXT_INT


    tomorrow's Report:

    c:\>powershell -File C:\uc4\V112\agents\V112_WIN01\Resources\0001\0001531006\0001531006_0001.ps1
    friday
    Output:  FR
    Function RC:  15

    Statistics:
    07.09.2017 19:36:10 -  U00011003 Job 'JOBS.WIN.PS.ERROR_IF_ADVANCED' (RunID '0001531006') on Host 'V112_WIN01' aborted (return code='0000000015'). Remote ID=3640





  • 3.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 07, 2017 02:45 PM
    I've put this chunk of code in an INCLUDE object that I can add to my PowerShell scripts;

    $RC = $?
    echo "POWERSHELL RC=$RC"
    switch ($RC)
        {
            True    { $EXIT_CODE = 0 }
            False   { $EXIT_CODE = 1 ; echo "Aborting script!" ; exit $EXIT_CODE }
            default { $EXIT_CODE = 1 ; echo "Aborting Script!" ; exit $EXIT_CODE }
        }


  • 4.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 08, 2017 12:52 AM

    Basically powershell can use exit statements to actively signal back returncodes, also throw() and catch() statements are available. Another way that can cause powershell scripts to end are the $ErrorActionsPreference, $WarningPreference, or even $DebugPreference and $VerbosePreference. These variables define how the script should act if an error, warning, debug / verbose message is being printed (write-error, -warning, -debug, -verbose). If the value is set to STOP, even verbose messages might cause the script to (internally) end with $LASTEXITCODE of -1.

    Anyway the issue will be, that non-"exit"-statements will not reach the upperlying layer.

    For those that haven't seen it yet, here's my blogpost (English version, German version is available as well) on PhilippElmers blog on an upcoming eLearning that enables you to use Powershell Cmdlets to access the AE:

    https://philippelmer.com/gastbeitrag-automatisieren-sie-die-automatisierung/



  • 5.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 08, 2017 02:22 AM
    Thanks for your suggestions, I did not make myself clear. I can check the exit status of commands, this is easy as some of you have pointed out with different methods.
    Unfortunately, any solution involving code will not work as the script won't be parsed.
    If there is a syntax error powershell will exit with zero but the script won't be executed at all.


  • 6.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 08, 2017 02:48 AM

    Hi Marco

    Syntax errors are problematic as they prevent a basic parsing that no try/catch blocks or traps are considered.

    What you can do is a precheck even tough this is a workaround and in complex scripts where other scripts are sourced it won't work. Sorry for the strange formatting.

    try { $null = [System.Management.Automation.ScriptBlock]::Create((Get-Content c:\temp\syntaxissue.ps1)) } catch { "There are syntax errors." }

    //Edit: the above will not execute any code in the script 

     



  • 7.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 08, 2017 04:27 AM
    I tried putting an exist statement after every PS command - possibly this could be an option:

    command1
    if (!$?) {$RC = 55; $RTEXT = "Syntax Error"; write-host "ERROR: $RC $RTEXT"; exit $RC }

    command2
    if (!$?) {$RC = 55; $RTEXT = "Syntax Error"; write-host "ERROR: $RC $RTEXT"; exit $RC }

    ......




  • 8.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 09, 2017 07:41 AM
    Just curious but are you using also the 2 command lines that are provided in Windows Job template to check the error level ?

    As you know Windows is not doing things as any other OS and use a variable %errorlevel% to indicate the return code value and not a system value .... just to make things simpler for checking result of a command  :) So all comaands are returning a code 0 regardless of the errorlevel value  :/


  • 9.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 03:20 AM
    Hi,
    yes, I am checking the return code for every command in the script, that works. Any method works.
    The issue is: if there is a syntax error the job still ends ok. You can try yourself a simple win job with an if statement and unbalanced parenthesis.


  • 10.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 03:51 AM

    By the way - I just tested at home with Powershell V5 - there syntax errors return RC 1.



  • 11.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 05:10 AM
    Marco,

    do you have an example of such a job that does not fail ?


  • 12.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 02:08 PM
    I have seen this happen several times and the false "ended_ok" can be problematic if it goes unnoticed. To prevent this, I usually put this on the Post Process tab of the JOBS. This scans the report for a keyword, then if the keyword is found on the report, the status is changed from ended ok to ended not ok.

    :SET &TEXT1 = PREP_PROCESS_REPORT(,,,'*InvalidOperation*')  <<or whatever keyword you want to use
    :PROCESS &TEXT1
    :  MODIFY_STATE RETCODE=02
    :  EXIT
    :ENDPROCESS
    :CLOSE_PROCESS &TEXT1

    Hope this helps.


  • 13.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 02:44 PM
    With some of our complex PowerShell scripts, rather than attempt to error check each statement, the PowerShell script puts out a "good end of script" message at the very end, and then I used a FILTER/OutputScan to check for that message in the results.  If the message is missing, then it sets the return code to a non-zero value.


  • 14.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 11, 2017 03:05 PM
    petwir I also tried using the filter/outputscan method, but sometimes it didn't catch the error, and it gave a false "ended_ok".


  • 15.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 12, 2017 09:38 AM
    Hi FrankMuffke ,
    you can try the code below, clearly it has unbalanced curl braces.
    if ($some_var) {
    }}}
    This is the last report:
    At F:\Automic\Agents\TEMP\JAAENZDR.TXT.ps1:11 char:3 + }}} +   ~ The Try statement is missing its Catch or Finally block. At F:\Automic\Agents\TEMP\JAAENZDR.TXT.ps1:11 char:3 + }}} +   ~ Unexpected token '}' in expression or statement. At F:\Automic\Agents\TEMP\JAAENZDR.TXT.ps1:13 char:1 + } + ~ Unexpected token '}' in expression or statement.     + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException     + FullyQualifiedErrorId : MissingCatchOrFinally

    This is the message in the log:
    9/12/2017 15:33:28 U00011002 Job 'JOBS.WIN.MARCO.TEST' (RunID '0002073387') on Host '******' ended normally. Remote ID=10104
    What JGi604607 suggested works, the issue here is that it may break easily by matching/not matching the report.


    Best,
    Marco




  • 16.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 12, 2017 10:37 AM

    By the way - I just tested at home with Powershell V5 - there syntax errors return RC 1.

    Same on my system - as soon as I enter your command, MarcoTizzoni604411
    if ($some_var) {}}}
    I get RC1:

    gasbsf338703.jpghttps://us.v-cdn.net/5019921/uploads/editor/3i/gasbsf338703.jpg" width="1226">

    So I assume, my PS version is above yours - I use 5.1


  • 17.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 12, 2017 10:47 AM
    Yeah, we have PS 4, maybe they fixed it in PS5, I am not able to test that.


  • 18.  Job return code is always zero when powershell script ha syntax error

    Posted Sep 12, 2017 10:55 AM
    You could install PS5.1, if its allowed by your company rules.

    https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell?view=powershell-5.1

    If not I am afraid you need to create a workaround.