5 Replies Latest reply on Mar 28, 2014 2:12 PM by DZ1

    Adding email to multiple alarms.  (Used Project Onyx to help create the bulk of the script)

    DZ1 Hot Shot

      First, I want to thank everyone who has ever helped me on these forums, I have really learned a lot, and special thanks to LucD, since he seems to superman down the most and save me. 

       


      Ok, here is my issue.  I used Project Onyx to create an alarm, and I was able to successfully take the output and change some things so that I could create alarms in multiple Datacenters.  Then I used Onyx again, to add an email notification, and I edited that out, but now I have a problem.  I created a csv file, so that each Datacenter will have a specific email account that will get notified, that works just fine, but the problem is when the script edits the alarm.

       


      I think the main problem is at the bottom in red, in Onyx it outputs the name of the user that made the change to the alarm ($spec.lastModifiedUser), and of course my account name was the one used.  There is also a timestamp ($spec.lastModifiedTime). 

       


      I've looked at some alarm scripting that LucD had as a post, but it's really out of my league.  I also tried to comment out some code for the user, but it appears to be mandatory, and I don't know how to dynamically make the username appear, or the timestamp.  I thinking that there is some way to take those sections out and create the alarm, but I don't know how to do that either.

       


        Here is the code.  The green part is stuff that I changed, the black is all onyx, and then the red is where I think the problem is. 


      if ( (Get-ExecutionPolicy) -ne "Unrestricted" ) {

       


      Write-Warning "Your Execution policy prohibits this script from running, please type this command at the Powershell prompt without the quotes: 'Set-ExecutionPolicy Unrestricted'"

       


      }

       


      #Checking for the Snapin to use PowerCLI cmdlets, the Snapin will be added if it's not already.

       


      if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {

       


      Add-PSSnapin -Name VMware.VimAutomation.Core

       


      }

       


      $EmailList = Import-Csv c:\List\EmailTest.csv

       


      Get-folder 'folder' | Get-Datacenter | foreach {

       


      $DC = $_

       


      if ( $_ | Get-AlarmDefinition | where { $_.Name -match "$($DC.Name)" + " Storage Alarm" } ) {

       


      $SpecAlarmVal = $DC.Id.Split("-",2)[1]

       


      $SpecEnVal = $DC.Id.Split("-",2)[1]

       


      $AlarmidName = (Get-AlarmDefinition -Entity $DC | where { $_.name -eq "$($DC.name)" + " Storage Alarm" } ).id

       

       

      1. ------- QueryOptions -------</div>


      $_this = Get-View -Id 'OptionManager-VpxSettings'

       


      $_this.QueryOptions("mail.smtp.server")

       

       

      1. ------- QueryOptions -------</div>


      $_this = Get-View -Id 'OptionManager-VpxSettings'

       


      $_this.QueryOptions("mail.sender")

       

       

      1. ------- ReconfigureAlarm -------</div>


      $spec = New-Object VMware.Vim.AlarmInfo

       


      $spec.name = "$($DC.Name)" + " Storage Alarm"

       


      $spec.description = "$($DC.Name)" + " Custom Storage Alarm"

       


      $spec.enabled = $true

       


      $spec.expression = New-Object VMware.Vim.OrAlarmExpression

       


      $spec.expression.expression = New-Object VMware.Vim.AlarmExpression[] (1)

       


      $spec.expression.expression[0] = New-Object VMware.Vim.MetricAlarmExpression

       


      $spec.expression.expression[0].operator = "isAbove"

       


      $spec.expression.expression[0].type = "Datastore"

       


      $spec.expression.expression[0].metric = New-Object VMware.Vim.PerfMetricId

       


      $spec.expression.expression[0].metric.counterId = 176

       


      $spec.expression.expression[0].metric.instance = ""

       


      $spec.expression.expression[0].yellow = 7500

       


      $spec.expression.expression[0].red = 9000

       


      $spec.action = New-Object VMware.Vim.GroupAlarmAction

       


      $spec.action.action = New-Object VMware.Vim.AlarmAction[] (1)

       


      $spec.action.action[0] = New-Object VMware.Vim.AlarmTriggeringAction

       


      $spec.action.action[0].action = New-Object VMware.Vim.SendEmailAction

       


      $spec.action.action[0].action.toList = ($EmailList | where { $_ -match "$($DC.name)" } | select Email).Email

       


      $spec.action.action[0].action.ccList = ""

       


      $spec.action.action[0].action.subject = ""

       


      $spec.action.action[0].action.body = ""

       


      $spec.action.action[0].transitionSpecs = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec[] (2)

       


      $spec.action.action[0].transitionSpecs[0] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

       


      $spec.action.action[0].transitionSpecs[0].startState = "yellow"

       


      $spec.action.action[0].transitionSpecs[0].finalState = "red"

       


      $spec.action.action[0].transitionSpecs[0].repeats = $false

       


      $spec.action.action[0].transitionSpecs[1] = New-Object VMware.Vim.AlarmTriggeringActionTransitionSpec

       


      $spec.action.action[0].transitionSpecs[1].startState = "green"

       


      $spec.action.action[0].transitionSpecs[1].finalState = "yellow"

       


      $spec.action.action[0].transitionSpecs[1].repeats = $false

       


      $spec.action.action[0].green2yellow = $false

       


      $spec.action.action[0].yellow2red = $false

       


      $spec.action.action[0].red2yellow = $false

       


      $spec.action.action[0].yellow2green = $false

       


      $spec.setting = New-Object VMware.Vim.AlarmSetting

       


      $spec.setting.toleranceRange = 0

       


      $spec.setting.reportingFrequency = 0

       


      $spec.key = ""

       


      $spec.alarm = New-Object VMware.Vim.ManagedObjectReference

       


      $spec.alarm.type = "Alarm"

       


      $spec.alarm.Value = $SpecAlarmVal

       


      $spec.entity = New-Object VMware.Vim.ManagedObjectReference

       


      $spec.entity.type = "Datacenter"

       


      $spec.entity.Value = $SpecEnVal

       


      $spec.lastModifiedTime = https://communities.vmware.com/System.DateTime::Parse("2014-03-26T17:25:13.456628Z")


      $spec.lastModifiedUser = "Domain\User"

       


      $spec.creationEventId = 0

       


      $_this = Get-View -Id "$AlarmidName"

       


      $_this.ReconfigureAlarm($spec)

       


      }

       


      }