You can start the try...catch option from the BlueParq Designer.
BlueParq Designer
- Press the right mouse button, the BlueParq Radial Menu will present itself.
- Press the Add try...catch button
Result in the BlueParq Designer
- You can now connect three blocks to the try...catch block:
- Try: here you add the PowerShell code which can generate an exception
- Catch: here you can catch the exception and continue your PowerShell script
- Final: Sometimes you don’t need to handle an error but still need some code to execute if an exception happens or not.
- In our example we will try to read a file from a network share which does not exist, this will generate an exception.
- We will Catch the exception and generate a message.
- Finally we will send the Final message.
- Press the right mouse button, the BlueParq Radial Menu will present itself.
- Choose the Add script blockand double click on the just created block.
- Add the following PowerShell code to it: [System.IO.File]::ReadAllText( '\\test\share\file_not_exist.log')
- Connect the script block to the top connector of the try...catch block.
- Press the right mouse button, the BlueParq Radial Menu will present itself.
- Press the Add cmdlet call button and press Echo in the search field. Also checkout the cmdlet wizard tutorial here.
- Press Echo under Alias and press Next.
- Make sure you add $_.Exception.ToString() in the InputObject text box.
- Press Finish and connect the cmdlet block to the middle connector of the try...catch block.
- Press the right mouse button, the BlueParq Radial Menu will present itself.
- Press the Add cmdlet call button and press Echo in the search field. Also checkout the cmdlet wizard tutorial here.
- Press Echo under Alias and press Next.
- Make sure you add Final in the InputObject text box.
- Press Finish and connect the cmdlet block to the lowest connector of the try...catch block.
Result in the BlueParq Editor
# The Try Catch block
Try
{
# Something will go wrong here
[System.IO.File]::ReadAllText( '\\test\share\file_not_exist.log')
}
Catch
{
# Show the exception message
Microsoft.PowerShell.Utility\Write-Output -InputObject $_.Exception.ToString()
}
Finally
{
# Final block
Microsoft.PowerShell.Utility\Write-Output -InputObject 'Final'
}