Hi,
I am trying to copy Alarms from one folder to other, After I copy the Alarms, I would like to change and modify Description and Email trigger email. how can I do this, please help!!
function Move-Alarm {
param($Alarm, $To, [switch]$DeleteOriginal = $false)
$alarm = Get-AlarmDefinition $Alarm
$alarmMgr = Get-View AlarmManager
$spec = New-Object -TypeName VMware.Vim.AlarmSpec
$spec.Name = $($alarm.Name).replace($strg1, $strg2)
$spec.Action = $alarm.ExtensionData.Info.Action
$spec.ActionFrequency = $alarm.ExtensionData.Info.ActionFrequency
$spec.Description = $alarm.ExtensionData.Info.Description
$spec.Enabled = $alarm.ExtensionData.Info.Enabled
$spec.Expression = $alarm.ExtensionData.Info.Expression
$spec.Setting = $alarm.ExtensionData.Info.Setting
$alarmMgr.CreateAlarm($to.ExtensionData.MoRef,$spec)
}
#$to = Get-DataCenter ####
$strg1 = "HR-Team-1"
$strg2 = "FIN-Team-2"
$to = Get-Folder -Name "Finance"
$tgtAlarm = "HR-Team-1 VM CPU Usage","HR-Team-1 VM Memory Usage"
$alarms = Get-AlarmDefinition -name $tgtAlarm
$alarms | % {
Move-Alarm -Alarm $_ -To $to -DeleteOriginal:$false
}
Have a look at Edit Alarm Action E-mail Recipients - VMware Technology Network VMTN to change the email recipient.
To change the Description use the Set-AlarmDefinition cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I made the changes as below, Description change is working but email update is not working.
function Move-Alarm {
param($Alarm, $To, [switch]$DeleteOriginal = $false)
$alarm = Get-AlarmDefinition $Alarm
$alarmMgr = Get-View AlarmManager
$spec = New-Object -TypeName VMware.Vim.AlarmSpec
$spec.Name = $($alarm.Name).replace($strg1, $strg2)
$spec.Action = $alarm.ExtensionData.Info.Action
$spec.ActionFrequency = $alarm.ExtensionData.Info.ActionFrequency
$spec.Description = $alarm.ExtensionData.Info.Description.replace($strg1, $strg2)
$spec.Enabled = $alarm.ExtensionData.Info.Enabled
$spec.Expression = $alarm.ExtensionData.Info.Expression
$spec.Setting = $alarm.ExtensionData.Info.Setting
$alarmMgr.CreateAlarm($to.ExtensionData.MoRef,$spec)
}
$strg1 = "HR-Team-1"
$strg2 = "FIN-Team-2"
$to = Get-Folder -Name "Finance"
$oldEmail = @("abc@domain.com","xyz@domain.com")
$newEmail = @("efg@domain.com","uvw@domain.com")
$tgtAlarm = "HR-Team-1 VM CPU Usage","HR-Team-1 VM Memory Usage"
$alarms = Get-AlarmDefinition -name $tgtAlarm
$alarms | % {
Move-Alarm -Alarm $_ -To $to -DeleteOriginal:$false
$mail = $_ | where {$_.ActionType -eq 'SendEmail' -and $_.To -contains $oldEmail}
Remove-AlarmAction -AlarmAction $mail -Confirm:$false
New-AlarmAction -AlarmDefinition $alarm -Email -To $newEmail -Subject $mail.Subject -Confirm:$false
}
Error
Remove-AlarmAction : Cannot bind argument to parameter 'AlarmAction' because it is null.
At D:\Date\Alarms\3_Copy_Alarms_To_different_Folder.ps1:32 char:33
+ Remove-AlarmAction -AlarmAction $mail -Confirm:$false
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-AlarmAction], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.RemoveAlarmAction
If there is no email action on the Alarm, you shouldn't remove it
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, I want to replace email address. how can i do it ?
There is no Replace function for an Alarm action, that is why I pointed to Edit Alarm Action E-mail Recipients - VMware Technology Network VMTN
This does a Remove and an Add, but you have to make sure that Where-clause to find the Action is correct.
In your previous snippet is had an error because you probably were not having the correct condition.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I tried using below, this works, if I have only "To" 1 email address and that can be replaced.
But If, I have multiple To email address, where If I need to replace only 1 email out of two, the below script, dont work.
Below doesn't work
$oldEmail = "QACS@domain.com", "QA2@domain.com"
$newemail = "xyz@domain.com"
Below script works for single email change
$tgtAlarm = "QACS VM CPU Usage","QACS VM Memory Usage"
$alarms = Get-AlarmDefinition -Name $tgtAlarm
$oldEmail = "QACS@domain.com"
$newemail = "xyz@domain.com"
foreach($alarm in $alarms){
foreach($action in (Get-AlarmAction -AlarmDefinition $alarm)){
if($action.ActionType -eq 'SendEmail'){
if($action.To.Split(',') -contains $oldEmail){
$newTo = @($action.To.Split(',') | %{
if($_ -eq $oldEmail){
$newemail
}
else{
$_
}
})
}
if($action.To -ne $newTo){
Remove-AlarmAction -AlarmAction $action -Confirm:$false
New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subject $mail.Subject -Confirm:$false
}
}
}
}
Like I said before, the Where-clause should be correct.
If you only need to replace 1 email address from multiple addresses, use the -contains operator to find the action.
Then use the Replace method on the array of Strings to replace that email address
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference