Hello
I have created one alarm on one VM, I want to copy same alarm info/configuration to other VM but we don't have folder for any VMs, can you help on the script level how to automate this?
thanks
You can use it copy alarms from one VM to another VM.
But note that in that case the Alarm(s) need to be defined on the VM.
function Move-Alarm{
param($Alarm, $From, $To, [switch]$DeleteOriginal = $false)
$alarmObj = Get-View $Alarm
$alarmMgr = Get-View AlarmManager
if($deleteOriginal){
$alarmObj.RemoveAlarm()
}
else{
$updateAlarm = New-Object VMware.Vim.AlarmSpec
$updateAlarm = $alarmObj.Info
$oldName = $alarmObj.Info.Name
$oldState = $alarmObj.Info.Enabled
$oldDescription = $alarmObj.Info.Description
$suffix = " (moved to " + ([string]($to | %{$_.Name + ","})).TrimEnd(",") + ")"
if(($oldName.Length + $suffix.Length) -gt $alarmLength){
$newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
}
else{
$newName = $oldName + $suffix
}
$updateAlarm.Name = $newName
$updateAlarm.Enabled = $false
$updateAlarm.Description += ("`rOriginal name: " + $oldName)
$updateAlarm.Expression.Expression | %{
if($_.GetType().Name -eq "EventAlarmExpression"){
$_.Status = $null
$needsChange = $true
}
}
$alarmObj.ReconfigureAlarm($updateAlarm)
$alarmObj.Info.Name = $oldName
$alarmObj.Info.Enabled = $oldState
$alarmObj.Info.Description = $oldDescription
}
$newAlarm = New-Object VMware.Vim.AlarmSpec
$newAlarm = $alarmObj.Info
$oldName = $alarmObj.Info.Name
$oldDescription = $alarmObj.Info.Description
foreach($destination in $To){
if($To.Count -gt 1){
$suffix = " (" + $destination.Name + ")"
if(($oldName.Length + $suffix.Length) -gt $alarmLength){
$newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
}
else{
$newName = $oldName + $suffix
}
$newAlarm.Name = $newName
$newAlarm.Description += ("`rOriginal name: " + $oldName)
}
$newAlarm.Expression.Expression | %{
if($_.GetType().Name -eq "EventAlarmExpression"){
$_.Status = $null
$needsChange = $true
}
}
$alarmMgr.CreateAlarm($destination.MoRef,$newAlarm)
$newAlarm.Name = $oldName
$newAlarm.Description = $oldDescription
}
}
# ============= Change values ============
$alarmName = 'Test Alarm on VM'
$fromVM = 'VM1'
$toVM = 'VM2'
# ========================================
Set-Variable -Name alarmLength -Value 80 -Option "constant"
$from = Get-VM -Name $fromVM | Get-View
$to = Get-VM -Name $toVM | Get-View
$alarmMgr = Get-View AlarmManager
$alarmMgr.GetAlarm($from.MoRef) | where{(Get-View -Id $_).Info.Name -eq $alarmName} | %{
Move-Alarm -Alarm $_ -From (Get-View $_) -To $to -DeleteOriginal:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Have a look at Alarms – Moving them around
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes but bit confused, instead of folder name, do I need to give VM name?in from and to field
You can use it copy alarms from one VM to another VM.
But note that in that case the Alarm(s) need to be defined on the VM.
function Move-Alarm{
param($Alarm, $From, $To, [switch]$DeleteOriginal = $false)
$alarmObj = Get-View $Alarm
$alarmMgr = Get-View AlarmManager
if($deleteOriginal){
$alarmObj.RemoveAlarm()
}
else{
$updateAlarm = New-Object VMware.Vim.AlarmSpec
$updateAlarm = $alarmObj.Info
$oldName = $alarmObj.Info.Name
$oldState = $alarmObj.Info.Enabled
$oldDescription = $alarmObj.Info.Description
$suffix = " (moved to " + ([string]($to | %{$_.Name + ","})).TrimEnd(",") + ")"
if(($oldName.Length + $suffix.Length) -gt $alarmLength){
$newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
}
else{
$newName = $oldName + $suffix
}
$updateAlarm.Name = $newName
$updateAlarm.Enabled = $false
$updateAlarm.Description += ("`rOriginal name: " + $oldName)
$updateAlarm.Expression.Expression | %{
if($_.GetType().Name -eq "EventAlarmExpression"){
$_.Status = $null
$needsChange = $true
}
}
$alarmObj.ReconfigureAlarm($updateAlarm)
$alarmObj.Info.Name = $oldName
$alarmObj.Info.Enabled = $oldState
$alarmObj.Info.Description = $oldDescription
}
$newAlarm = New-Object VMware.Vim.AlarmSpec
$newAlarm = $alarmObj.Info
$oldName = $alarmObj.Info.Name
$oldDescription = $alarmObj.Info.Description
foreach($destination in $To){
if($To.Count -gt 1){
$suffix = " (" + $destination.Name + ")"
if(($oldName.Length + $suffix.Length) -gt $alarmLength){
$newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
}
else{
$newName = $oldName + $suffix
}
$newAlarm.Name = $newName
$newAlarm.Description += ("`rOriginal name: " + $oldName)
}
$newAlarm.Expression.Expression | %{
if($_.GetType().Name -eq "EventAlarmExpression"){
$_.Status = $null
$needsChange = $true
}
}
$alarmMgr.CreateAlarm($destination.MoRef,$newAlarm)
$newAlarm.Name = $oldName
$newAlarm.Description = $oldDescription
}
}
# ============= Change values ============
$alarmName = 'Test Alarm on VM'
$fromVM = 'VM1'
$toVM = 'VM2'
# ========================================
Set-Variable -Name alarmLength -Value 80 -Option "constant"
$from = Get-VM -Name $fromVM | Get-View
$to = Get-VM -Name $toVM | Get-View
$alarmMgr = Get-View AlarmManager
$alarmMgr.GetAlarm($from.MoRef) | where{(Get-View -Id $_).Info.Name -eq $alarmName} | %{
Move-Alarm -Alarm $_ -From (Get-View $_) -To $to -DeleteOriginal:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Lucd,
Can you tell me where should I put for folder , I want to copy alarm from folder to another folder within same datacneter
That is done through the To parameter, it can have multiple values.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference