Getting Release Management to fail a release when using a custom PowerShell component

If you have a custom PowerShell script you wish to run you can create a tool in release Management (Inventory > Tools) for the script which deploys the .PS1, PSM files etc. and defines the command line to run it.

The problem we hit was that our script failed, but did not fail the build step as the PowerShell.EXE running the script exited without error. The script had thrown an exception which was in the output log file, but it was marked as a completed step.

The solution was to use a try/catch in the .PS1  script that as well as writing a message to Write-Error also set the exit code to something other than 0 (Zero). So you end up with something like the following in your .PS1 file

 1param  
 2(  
 3\[string\]$Param1 ,  
 4\[string\]$Param2 ) 
 5
 6try  
 7{  
 8    # some logic here
 9
10 
11
12} catch  
13{  
14    Write-Error $\_.Exception.Message  
15    exit 1 # to get an error flagged so it can be seen by RM  
16}  

Once this change was made an exception in the PowerShell caused the release step to fail as required. The output from the script appeared as the Command Output.