Hi The following script move the virtual machines alarms from vCenter level to folder level. The script works with Powercli4.1 but not 5.1. It gives error message for the line $alarmMgr.CreateAl...
See more...
Hi The following script move the virtual machines alarms from vCenter level to folder level. The script works with Powercli4.1 but not 5.1. It gives error message for the line $alarmMgr.CreateAlarm($destination.MoRef,$newAlarm) Any idea why it doesnt work?? Write-Host "Adding in VMware PowerCLI PowerShell Snapin......" Add-PSSnapin vmware.VimAutomation.core -ErrorAction SilentlyContinue $viserver = "localhost" Write-Host "Connecting to vCenter: " $viserver Connect-VIServer $viserver Write-Host "Collecting VM and Root Folder information...." Set-Variable -Name alarmLength -Value 80 -Option "constant" $from = Get-Folder -NoRecursion | Get-View $to1 = Get-Folder -Name "Customer VMs" | Get-View #move alarms from vCenter level to Customer VMs folder level $to2 = Get-Folder -Name "Management VMs" | Get-View #move alarms from vCenter level to Management VMs folder level function Move-Alarm{ param($Alarm, $From, $To, [switch]$DeleteOriginal = $false, [switch]$VMAlarmsOnly = $false) $alarmObj = Get-View $Alarm $alarmMgr = Get-View AlarmManager $IsAVirtualMachine = $false $alarmObj.Info.Expression.Expression | % { #$_.Type if ($_.Type -eq "VirtualMachine") { $IsAVirtualMachine = $true } } if($VMAlarmsOnly){ if($IsAVirtualMachine) { 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 += ("`r`n Original 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 += ("`r`n Original 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 } } } else { 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 += ("`r`n Original 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 += ("`r`n Original 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 } } } Write-Host "Parsing Alarms and moving VM Alarms (if you see errors it is likely that you have run this script twice and may been to clean up the alarms)......" Write-Host Write-Host "Note: There should be a listed for each alarm copied from root to each of the two folders, as an example if there are 4 Alarms (Type: Virtual Machine) in the root you should see 8 alarms listed." Write-Host $alarmMgr = Get-View AlarmManager $alarms = $alarmMgr.GetAlarm($from.MoRef) $alarms | % { Move-Alarm -Alarm $_ -From (Get-View $_) -To $to1,$to2 -DeleteOriginal:$false -VMAlarmsOnly:$true } Disconnect-VIServer -confirm:$false Write-Host "Hit any key to close window....." #Pause Screen $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")