Advertisement
Guest User

Modified Dell OMSANotify.ps1

a guest
Mar 6th, 2017
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. This is a PowerShell Script to generate email alerts from Dell OpenManage Server Administrator Alerts
  4.  
  5. .DESCRIPTION
  6. This Script Is used to send SMTP alerts from Servers running Dell Open Manage Server Administrator. It can automatically configure itself for most common OpenManage Alerts using the -Setup Parameter. It can also send test alerts using -Test.
  7.  
  8. .PARAMETER Setup
  9. Runs omconfig commands to set the script as action on alert generation.
  10.  
  11. .PARAMETER Test
  12. Sends a test alert.
  13.  
  14. .PARAMETER eventType
  15. The Event Class to generate an Alert for.
  16.  
  17. .EXAMPLE
  18. ./OMSANotify.ps1 -Test
  19.  
  20.  
  21. .LINK
  22. https://bitbucket.org/ncouraud/omsa-notify
  23. #>
  24.  
  25. # Setup Our Parameters
  26. [CmdletBinding()]
  27. Param(
  28. # The Event Type that we need to respond to.
  29. [Parameter(Mandatory=$False,Position=1)]
  30. [string]$eventType,
  31.  
  32. # Run the Setup Commands
  33. [switch]$Setup,
  34.  
  35. # Send a Test Alert
  36. [switch]$Test
  37. )
  38.  
  39. # Define the List of Alerts that we Respond to. Desired Alerts May be Added/Removed.
  40. $Alerts = @{}
  41. $Alerts.Add("powersupply",'Power Supply Failure')
  42. $Alerts.Add("powersupplywarn",'Power Supply Warning')
  43. $Alerts.Add("tempwarn",'Temperature Warning')
  44. $Alerts.Add("tempfail",'Temperature Failure')
  45. $Alerts.Add("fanwarn",'Fan Speed Warning')
  46. $Alerts.Add("fanfail",'Fan Speed Failure')
  47. $Alerts.Add("voltwarn",'Voltage warning')
  48. $Alerts.Add("voltfail",'Voltage Failure')
  49. $Alerts.Add("Intrusion",'Chassis Intrusion')
  50. $Alerts.Add("redundegrad",'Redundancy Degraded')
  51. $Alerts.Add("redunlost",'Redundancy Lost')
  52. $Alerts.Add("memprefail",'Memory Pre-Failure')
  53. $Alerts.Add("memfail",'Memory Failure')
  54. $Alerts.Add("hardwarelogwarn",'Hardware Log Warning')
  55. $Alerts.Add("hardwarelogfull",'Hardware Log Full')
  56. $Alerts.Add("processorwarn",'Processor Warning')
  57. $Alerts.Add("processorfail",'Processor Failure')
  58. $Alerts.Add("watchdogasr",'Watchdog ASR')
  59. $Alerts.Add("batterywarn",'Battery Warning')
  60. $Alerts.Add("batteryfail",'Battery Failure')
  61. $Alerts.Add("systempowerwarn",'System Power Warning')
  62. $Alerts.Add("systempowerfail",'System Power Failure')
  63. $Alerts.Add("storagesyswarn",'Storage System Warning')
  64. $Alerts.Add("storagesysfail",'Storage System Failure')
  65. $Alerts.Add("storagectrlwarn",'Storage Controller Warning')
  66. $Alerts.Add("storagectrlfail",'Storage Controller Failure')
  67. $Alerts.Add("pdiskwarn",'Physical Disk Warning')
  68. $Alerts.Add("pdiskfail",'Physical Disk Failure')
  69. $Alerts.Add("vdiskwarn",'Virtual Disk Warning')
  70. $Alerts.Add("vdiskfail",'Virtual Disk Failure')
  71. $Alerts.Add("enclosurewarn",'Enclosure Warning')
  72. $Alerts.Add("enclosurefail",'Enclosure Failure')
  73. $Alerts.Add("storagectrlbatterywarn",'Storage Controller Battery Warning')
  74. $Alerts.Add("storagectrlbatteryfail",'Storage Controller Battery Failure')
  75. $Alerts.Add("test",'Test Alert')
  76.  
  77. # Sends our Alert Mail
  78. function sendMail($AlertType, $body) {
  79.  
  80. #Create remote session to send mail from Server with Public IP
  81. $option = New-PSSessionOption -ProxyAccessType NoProxyServer
  82. $pw = "password" | ConvertTo-SecureString -AsPlainText -Force
  83. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$pw
  84. $RoutableServer = New-PSSession public-server.example.com -SessionOption $option -credential $cred
  85. $ScriptBlockContent = {
  86. param(
  87. [Parameter(Position=0)]
  88. $one
  89. ,
  90. [Parameter(Position=1)]
  91. $two
  92. ,
  93. [Parameter(Position=2)]
  94. $three
  95. )
  96.  
  97. #Creating a Mail object
  98. $msg = new-object Net.Mail.MailMessage
  99.  
  100. #Email structure
  101. $msg.From = "office365account@example.com"
  102. $msg.ReplyTo = "No-Reply@example.com"
  103. $msg.To.Add("youremail@example.com")
  104. $msg.Subject = "Dell OMSA $two Alert on $three"
  105. $msg.body = $one
  106.  
  107.  
  108. #SMTP server name
  109. $smtpServer = "smtp.office365.com"
  110. $smtpPort = "587"
  111. $smtpUser = "office365account@example.com"
  112. $smtpPass = "password"
  113.  
  114. #Creating SMTP server object
  115. $smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
  116. $smtp.EnableSsl = $true
  117. $smtp.Credentials = New-Object System.Net.NetworkCredential( $smtpUser , $smtpPass );
  118.  
  119. #Sending email
  120. $smtp.Send($msg)}
  121. Invoke-Command -Session $RoutableServer -ScriptBlock $ScriptBlockContent -ArgumentList $body, $AlertType, $env:COMPUTERNAME
  122. Remove-PSSession $RoutableServer
  123.  
  124. }
  125.  
  126. # Kicks Off OM Alert Config Commands for all Warnings/Failures
  127. Function Setup(){
  128. # Define our command String
  129. $ScriptPath = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Definition
  130. $command = "powershell "+$ScriptPath+" -eventType"
  131.  
  132. # Set Up OpenManage Alert handlers
  133. SetOMAlert "powersupply" $command;
  134. SetOMAlert "powersupplywarn" $command;
  135. SetOMAlert "tempwarn" $command;
  136. SetOMAlert "tempfail" $command;
  137. SetOMAlert "fanwarn" $command;
  138. SetOMAlert "fanfail" $command;
  139. SetOMAlert "voltwarn" $command;
  140. SetOMAlert "voltfail" $command;
  141. SetOMAlert "Intrusion" $command;
  142. SetOMAlert "redundegrad" $command;
  143. SetOMAlert "redunlost" $command;
  144. SetOMAlert "memprefail" $command;
  145. SetOMAlert "memfail" $command;
  146. SetOMAlert "hardwarelogwarn" $command;
  147. SetOMAlert "hardwarelogfull" $command;
  148. SetOMAlert "processorwarn" $command;
  149. SetOMAlert "processorfail" $command;
  150. SetOMAlert "watchdogasr" $command;
  151. SetOMAlert "batterywarn" $command;
  152. SetOMAlert "batteryfail" $command;
  153. SetOMAlert "systempowerwarn" $command;
  154. SetOMAlert "systempowerfail" $command;
  155. SetOMAlert "storagesyswarn" $command;
  156. SetOMAlert "storagesysfail" $command;
  157. SetOMAlert "storagectrlwarn" $command;
  158. SetOMAlert "storagectrlfail" $command;
  159. SetOMAlert "pdiskwarn" $command;
  160. SetOMAlert "pdiskfail" $command;
  161. SetOMAlert "vdiskwarn" $command;
  162. SetOMAlert "vdiskfail" $command;
  163. SetOMAlert "enclosurewarn" $command;
  164. SetOMAlert "enclosurefail" $command;
  165. SetOMAlert "storagectrlbatterywarn" $command;
  166. SetOMAlert "storagectrlbatteryfail" $command;
  167.  
  168. # Register Our Event Log Source
  169. if ([System.Diagnostics.EventLog]::SourceExists("OMSANotify") -eq $false) {
  170. [System.Diagnostics.EventLog]::CreateEventSource("OMSANotify", "System")
  171. }
  172. }
  173.  
  174. # OMCONFIG Runner for individual Alert config
  175. Function SetOMAlert($event, $cmdString){
  176. invoke-command -scriptblock {omconfig system alertaction event=$Event execappath="$cmdString $event"}
  177. }
  178.  
  179. # Lets Generate A Test case Email, so we can be sure it works
  180. Function Test(){
  181. ProcessAlert "test";
  182. }
  183.  
  184. # Logs OMSA Event and Email in Windows Event Log
  185. Function logEvent($event)
  186. {
  187. $EventMsg = "OMSA Notify Processed Dell Open Manage Event $event"
  188. Write-EventLog -Logname System -Source OMSANotify -eventId 1 -entryType Warning -message $EventMsg
  189. }
  190.  
  191. # Handles All Alert Processing.
  192. Function ProcessAlert($alert) {
  193. $AlertMessageString = ""
  194.  
  195. # Check if it's a known OMSA Alert
  196. If($Alerts.containsKey($alert)){
  197. $AlertMessageString = $Alerts.Get_Item($alert) + " was reported on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
  198. }
  199. Else {
  200. "Unknown Alert - $alert was reported at $Date on $Env:COMPUTERNAME. Please log in ASAP and check OMSA for further details."
  201. }
  202.  
  203. sendMail $alert $AlertMessageString;
  204.  
  205. #Register our event in Windows Event Log.
  206. logEvent $alert;
  207. }
  208.  
  209.  
  210. if($eventType) {
  211. ProcessAlert $eventType;
  212. }
  213. else {
  214. if($Setup) {
  215. Setup;
  216. }
  217. if($Test) {
  218. Test;
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement