Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. Param($action, $alertcode)
  2.  
  3. # Set the variables below to suit your environment. The script will default to using localhost and port 25 if neither value is specified
  4. # Change the $MailSubject as preferred "COMPUTERNAME" and "ALERTDETAIL" will be replaced with the appropriate values if left in place.
  5.  
  6. # For Gmail accounts:
  7. # Set SMTPServer = "smtp.gmail.com"
  8. # set SMTPUser and SMTPPassword to your gmail credentials, do not include the @gmail.com part in the username.
  9. # set SMTPPort = 587. Set EnableSSL = $true
  10.  
  11. ## Change variables here
  12. $EmailFrom = ""
  13. $EmailTo = ""
  14. $SMTPServer = ""
  15. $SMTPPort = ""
  16. $SMTPUser = ""
  17. $SMTPPassword = ""
  18. $EnableSSL = $false
  19. $MailSubject = "Dell Server Alert on COMPUTERNAME - ALERTDETAIL"
  20. ## End of variables
  21.  
  22. function SetDellAlert
  23. {
  24. Param($DellEvent, $DellEventDescription, $EventID)
  25. $omsacommand = "omconfig system alertaction event=$DellEvent execappath=`"$powershellpath -executionpolicy unrestricted $scriptpath email $EventID`" "
  26. "Configuring Alert: $DellEvent ($DellEventDescription)"
  27. Invoke-Expression $omsacommand
  28. }
  29.  
  30. function SendAlert($Subject)
  31. {
  32. $MailClient = new-object system.net.mail.smtpClient
  33. $MailClient.EnableSSL = $EnableSSL
  34.  
  35. if ($SMTPServer -eq "") {$SMTPServer = "localhost"}
  36. if ($SMTPPort -eq "") {$SMTPPort = "25"}
  37. if ($SMTPUser -ne "")
  38. {
  39. $MailClient.UseDefaultCredentials = "false"
  40. $MailClient.Credentials = New-Object System.Net.NetworkCredential($SMTPUser, $SMTPPassword)
  41. }
  42.  
  43. $MailMessage = New-Object system.net.mail.mailmessage
  44. $MailMessage.from = ($EmailFrom)
  45. $MailMessage.to.add($EmailTo)
  46. $MailMessage.subject = $Subject
  47. $MailMessage.body = ""
  48. $MailClient.Host = $SMTPServer
  49. $MailClient.Port = $SMTPPort
  50. $MailClient.Send($MailMessage)
  51. }
  52.  
  53. Function CreateMessageBody
  54. {
  55. $result = invoke-expression "omreport chassis info"
  56. $result = $result + (invoke-expression "omreport system alertlog")
  57. $result
  58. }
  59.  
  60. $AlertConfig = "powersupply|Power supply failure", "powersupplywarn|Power supply warning", "tempwarn|Temperature warning", "tempfail|Temperature failure", "fanwarn|Fan speed warning", "fanfail|Fan speed failure", "voltwarn|Voltage warning", "voltfail|Voltage failure", "intrusion|Chassis intrusion", "redundegrad|Redundancy degraded", "redunlost|Redundancy lost", "memprefail|Memory pre-failure", "memfail|Memory failure", "hardwarelogwarn|Hardware log warning", "hardwarelogfull|Hardware log full", "processorwarn|Processor warning", "processorfail|Processor failure", "watchdogasr|Watchdog asr", "batterywarn|Battery warning", "batteryfail|Battery failure", "systempowerwarn|Power warning", "systempowerfail|Power failure", "systempeakpower|Peak power", "removableflashmediapresent|Removable flash media present", "removableflashmediaremoved|Removable flash media removed", "removableflashmediafail|Removable flash media failure", "storagesyswarn|Storage System warning", "storagesysfail|Storage System failure", "storagectrlwarn|Storage Controller warning", "storagectrlfail|Storage Controller failure", "pdiskwarn|Physical Disk warning", "pdiskfail|Physical Disk failure", "vdiskwarn|Virtual Disk warning", "vdiskfail|Virtual Disk failure", "enclosurewarn|Enclosure warning", "enclosurefail|Enclosure failure", "storagectrlbatterywarn|Storage Controller Battery warning", "storagectrlbatteryfail|Storage Controller Battery failure"
  61.  
  62. $ComputerName = gc env:computername
  63.  
  64. $a = New-Object -ComObject Scripting.FileSystemObject
  65. $f = $a.GetFile("$pshome\powershell.exe")
  66. $powershellpath = $f.Path
  67. $fullscriptpath=$MyInvocation.Mycommand.path
  68. $f = $a.GetFile($fullscriptpath)
  69. $scriptpath=$f.shortpath
  70.  
  71. if ($action)
  72. {
  73. $action = $action.trim().tolower()
  74. switch ($action)
  75. {
  76. "setup"
  77. {
  78. for ($i = 0; $i -lt $AlertConfig.count; $i++)
  79. {
  80. $AlertDetails = $AlertConfig[$i].split("|")
  81. SetDellAlert $AlertDetails[0] $AlertDetails[1] $i
  82. }
  83. }
  84. "email"
  85. {
  86. $AlertDetails = $AlertConfig[$alertcode].split("|")
  87. $MailSubject = $MailSubject -replace('COMPUTERNAME', $ComputerName)
  88. $MailSubject = $MailSubject -replace('ALERTDETAIL', $AlertDetails[1])
  89. Sendalert $MailSubject
  90. }
  91. "testemail"
  92. {
  93. $MailSubject = $MailSubject -replace('COMPUTERNAME', $ComputerName)
  94. $MailSubject = $MailSubject -replace('ALERTDETAIL', "Test Alert")
  95. Sendalert $MailSubject
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement