{"id":160,"date":"2016-02-24T01:29:28","date_gmt":"2016-02-24T01:29:28","guid":{"rendered":"http:\/\/www.mayerinformatics.com\/?p=160"},"modified":"2016-02-24T01:29:28","modified_gmt":"2016-02-24T01:29:28","slug":"scom-console-task-to-forward-alert-as-email","status":"publish","type":"post","link":"https:\/\/www.mayerinformatics.com\/?p=160","title":{"rendered":"SCOM Console Task to Forward Alert as Email"},"content":{"rendered":"<h2>Problem:<\/h2>\n<p>Some alerts in SCOM are considered incidents, in ITIL terminology, and should be tracked in a\u00a0ticketing 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.<\/p>\n<h2>Solution:<\/h2>\n<p>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 <a href=\"http:\/\/blog.tyang.org\/2012\/08\/16\/scom-enhanced-email-notification-script-version-2\/\" target=\"_blank\">Tao Yang&#8217;s script<\/a>. I ended up modifying his script in order to fit my needs.<\/p>\n<p>Tao&#8217;s script was designed to be a replacement for SCOM email notifications. That was not how I wanted to implement it.<\/p>\n<p>The first changes were to the parameters<\/p>\n<pre class=\"brush: powershell; light: true; title: ; notranslate\" title=\"\">\nparam(\n &#x5B;Parameter(Mandatory=$True,Position=1)]\n &#x5B;string]$alertID,\n &#x5B;Parameter(Mandatory=$True,Position=2)]\n &#x5B;string]$WebConsoleLink,\n &#x5B;Parameter(Mandatory=$True,Position=3)]\n &#x5B;string&#x5B;]]$Recipients,\n &#x5B;Parameter(Mandatory=$False,Position=4)]\n &#x5B;string]$ScriptLocation\n )\n<\/pre>\n<p>The changes allowed parameters to be passed is as positional parameters. The $ScriptLocation parameter was added because Tao&#8217;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.<\/p>\n<p>I also made modifications to deal with $myInvocation.MyCommand.Path. I replaced $MyInvocation with $PSScriptRoot, which is a powershell v3+ feature. I don&#8217;t plan on using $PSScriptRoot, but I left it in case I ended up using it.<\/p>\n<pre class=\"brush: powershell; light: true; title: ; notranslate\" title=\"\">\n#$MyInvocation has been superceeded in PS v3.\n#$thisScript = $myInvocation.MyCommand.Path\n\n#PSScriptRoot does not work when executed through invoke-command. If the $ScriptLocation is passed through as a variable we will not derive the\n# the location from $PSScriptRoot\nif (!($ScriptLocation)){\n$thisScript = $PSScriptRoot\n}\nelseif ($ScriptLocation){\n$thisScript = $ScriptLocation\n}\n\n$scriptRoot = Split-Path(Resolve-Path $thisScript)\n$errorLogFile = Join-Path $scriptRoot &quot;error.log&quot;\n<\/pre>\n<p>In the authoring pain I created an &#8220;alert command line&#8221; console task. The application I call is &#8220;%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe&#8221; and the Parameters<\/p>\n<pre class=\"brush: powershell; light: true; title: ; notranslate\" title=\"\">\n\n-command Invoke-Command -ArgumentList &quot;$ID$&quot;,&quot;http:\/\/servername\/WebConsoleUrl$?DisplayMode=Pivot''&amp;''AlertID=$ID$&quot;,&quot;\\\\servername\\c$\\Scripts\\SCOMEnhancedEmailNotification.ps1&quot;,@('recipient;recipient@yourcompany.com') -ComputerName servername\u00a0 \u00a0-FilePath &quot;\\\\servername\\c$\\Scripts\\SCOMEnhancedEmailNotification.ps1&quot;\n\n<\/pre>\n<p>Where you see &#8220;servername&#8221; insert your SCOM servername. The recipients email and name will also need to be defined.<\/p>\n<p><a href=\"https:\/\/wordpress.n0rmality.net\/wp-content\/uploads\/2016\/01\/SCOMEnhancedEmailNotification.ps1_.zip\">Download<\/a> modified script<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Some alerts in SCOM are considered incidents, in ITIL terminology, and should be tracked in a\u00a0ticketing 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":0,"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mayerinformatics.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}