Problem:
Some alerts in SCOM are considered incidents, in ITIL terminology, and should be tracked in a ticketing system. The easiest way to get a SCOM alert into appeared to be email. I did not want all alerts becoming tickets, just the ones I decided required tracking.
Solution:
On a cursory search it seemed easy enough run a powershell script from the SCOM console to send an email. However, any console task required that the powershell script be located on all of our servers that run the console. I wanted to be able to manage the script centrally. In my search for how to accomplish sending emails from scom, I came across Tao Yang’s script. I ended up modifying his script in order to fit my needs.
Tao’s script was designed to be a replacement for SCOM email notifications. That was not how I wanted to implement it.
The first changes were to the parameters
param( [Parameter(Mandatory=$True,Position=1)] [string]$alertID, [Parameter(Mandatory=$True,Position=2)] [string]$WebConsoleLink, [Parameter(Mandatory=$True,Position=3)] [string[]]$Recipients, [Parameter(Mandatory=$False,Position=4)] [string]$ScriptLocation )
The changes allowed parameters to be passed is as positional parameters. The $ScriptLocation parameter was added because Tao’s script used $myInvocation.MyCommand.Path to determine the location of the script. With the powershell command invoke-command this does not work. Therefore, the location of the script needs to be passed in as a parameter.
I also made modifications to deal with $myInvocation.MyCommand.Path. I replaced $MyInvocation with $PSScriptRoot, which is a powershell v3+ feature. I don’t plan on using $PSScriptRoot, but I left it in case I ended up using it.
#$MyInvocation has been superceeded in PS v3. #$thisScript = $myInvocation.MyCommand.Path #PSScriptRoot does not work when executed through invoke-command. If the $ScriptLocation is passed through as a variable we will not derive the # the location from $PSScriptRoot if (!($ScriptLocation)){ $thisScript = $PSScriptRoot } elseif ($ScriptLocation){ $thisScript = $ScriptLocation } $scriptRoot = Split-Path(Resolve-Path $thisScript) $errorLogFile = Join-Path $scriptRoot "error.log"
In the authoring pain I created an “alert command line” console task. The application I call is “%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe” and the Parameters
-command Invoke-Command -ArgumentList "$ID$","http://servername/WebConsoleUrl$?DisplayMode=Pivot''&''AlertID=$ID$","\\servername\c$\Scripts\SCOMEnhancedEmailNotification.ps1",@('recipient;recipient@yourcompany.com') -ComputerName servername -FilePath "\\servername\c$\Scripts\SCOMEnhancedEmailNotification.ps1"
Where you see “servername” insert your SCOM servername. The recipients email and name will also need to be defined.
Download modified script