Service Management Automation – Error Handling

In this blog I will give some examples of error handling within a InlineScript, in a Powershell workflow using SMA.
Normally you can define multiple workflows in Powershell, in combination with SMA there is a restriction to use more than one workflow per script.
Also you cannot execute any commands outside of the defined workflow, which can make error handling a little tricky.

First I have created a Powershell workflow which will receive “computer info” from the computer “TestVM” trough WMI.

workflow CreateError
{
    $Computer = "TestVM"
   
    InlineScript {
        Try {
            $ComputerInfo = Get-WMIObject -computername $using:computer -query "select TotalPhysicalMemory from Win32_ComputerSystem"
        }
        Catch {
            $ErrorMessage = $Error[0].Exception
        }
        $output = [PSCustomObject]@{
            Name = $using:computer
            TotalPhysicalMemory = $ComputerInfo.TotalPhysicalMemory
            Error = $ErrorMessage
        }
        Write-output $output
        }
}

This script will generate an “Access denied” error because we don’t have the appropriate rights.

In the script we will collect information like;
– The computer we are targeting
– The TotalPhysicalMemory we are getting back from the WMI query
– The errormessage if an error is catched.

This information is collected in a custom powershell object (PSCustomObject) and set in variable $output.
Then we will write the variable with “Write-output” to output the information to SMA.

In Windows Azure Pack it will look like this when the job is run:

SMA1-3

The output of the job contains the custom powershell object entries we’ve filled with information.
This way you can give any information back and show it in Windows Azure Pack, or use it to do some more like Error Handling.

When an error is catched it will be stored in the PSCustomObject.
You can do a simple powershell If statement to see if there’s an error stored in the PSCustomObject, and if so, output custom text instead of the complete error message:

workflow CreateError
{
    $Computer = "TestVM"
   
    InlineScript {
        Try {
            $ComputerInfo = Get-WMIObject -computername $using:computer -query "select TotalPhysicalMemory from Win32_ComputerSystem"
        }
        Catch {
            $ErrorMessage = $Error[0].Exception
        }
        $output = [PSCustomObject]@{
            Name = $using:computer
            TotalPhysicalMemory = $ComputerInfo.TotalPhysicalMemory
            Error = $ErrorMessage
        }
       
        If ($output.error){
        Write-Output "Dude! There was an error"
                          }
         
        }
}

What would look like this:
SMA2

Basically we can create any action if there’s an error..
Send an email

If ($output.error){
Send-MailMessage -From "SMA@domain.com" -To "administrator@domain.com" -Subject "Error in SMA Job" -Body "There was an error with $output.name and the error was $output.error"
          }

Write an Eventviewer event to a SCOM server, and add custom event viewer monitor (link)

If ($output.error){
Write-EventLog –LogName Application –Source "SMA" –EntryType Error –EventID 1 –Message "There was an error with $output.name and the error was $output.error" -ComputerName "SCOM1"
          }

Or whatever you like, and is able with powershell 🙂

Logging

In SMA there are multiple ways to output logging, we’ve already seen the “write-output” used above.
The write-output displays output only in the Output section on the job details:
SMA3

You can also use the following logging types:

Write-Warning
Write-Error
Write-Verbose

I’ve created a new Runbook that shows all the logging methods:

workflow CreateOutput
{
    Write-Output "Output Logging"
    Write-Warning "Warning Logging"
    Write-Error "Error Logging"
    Write-Verbose "Verbose Logging"
}

All Logging methods other than “Write-output” only show under the History tab:
SMA5

I think Verbose logging is very cool, you can output script details but only show it when you enable verbose logging in the runbook config:
SMA6

Terminating Errors

Did you notice the Job status in Windows Azure Pack is still “Completed”? while we have a major errors!
Jobs will only “stop” executing if there is an terminating error, there are two ways to create a terminating error:

Write-Error -ErrorAction Stop

Write-Warning -WarningAction Stop

Actually this will not Stop the job, but suspend it.
You could also use the “Throw” powershell method.
SMA7

In the job dashboard there will be shown an exeption in the exeption section:
SMA8

I did not found an method to actually stop the job from within the job.
After suspension, you can stop the job in Windows Azure Pack.

Thanks for reading and have fun with SMA!
If you have any comments or questions, drop me an email

Leave a Reply

Your email address will not be published. Required fields are marked *