Power shell script block action always returns 0

Document ID : KB000017214
Last Modified Date : 14/02/2018
Show Technical Document Details
Question:

Why do i always get back a return code  0 from the PowerShell Action Pack action "PowerShell - Run Script Block" ?

Even if an error occurred in the script block . 

Answer:

The "PowerShell - Run Script Block" Action will always return a zero return code unless the action block itself set the error code to something else .

The following Script Block example will set the return code to 12 if the delete command is failing 

----------------

del c:\xyz.txt 

if ( $error.count ) { 
exit 12 

---------------

This looks to be a incorrect syntax because the documentation for the action on docops says :

A PowerShell script-block (inline commands) to be run locally. For example, get-date; write-host "hello". Do not add curly braces {} to enclose the input.

The documentation is correct. What it says that you should not have as follows. 

Invalid examples :

/* script block */ 
Your script block goes here … 


E.g. 

del c:\xyz.txt 
if ( $error.count ) { 
exit 12 



Valid Examples :
But following is fine as you are using it as if condition. 
/* script block */ 
del c:\xyz.txt 
if ( $error.count ) { 
exit 12 


The  documentation says “Do NOT add curly braces {} to enclose the input”. 
Technically power shell will not execute this code and action will always pass as it doesn’t execute the code immediately.