Advertisement
Guest User

Untitled

a guest
Jan 27th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Set-Variable -Name alarmLength -Value 80 -Option "constant"
  2.  
  3. function Move-Alarm {
  4.   param($Alarm, $From, $To, [switch]$DeleteOriginal = $false)
  5.  
  6.   $alarmMgr = Get-View AlarmManager
  7.  
  8.   $alarmObj = Get-View $Alarm
  9.   $alarmMgr = Get-View AlarmManager
  10.   if($deleteOriginal){
  11.     $alarmObj.RemoveAlarm()
  12.   }
  13.   else{
  14.     $updateAlarm = New-Object VMware.Vim.AlarmSpec
  15.     $updateAlarm = $alarmObj.Info
  16.     $oldName = $alarmObj.Info.Name
  17.     $oldState = $alarmObj.Info.Enabled
  18.     $oldDescription = $alarmObj.Info.Description
  19.     $suffix = " (moved to " + ([string]($to | %{$_.Name + ","})).TrimEnd(",") + ")"
  20.     if(($oldName.Length + $suffix.Length) -gt $alarmLength){
  21.       $newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
  22.     }
  23.     else{
  24.       $newName = $oldName + $suffix
  25.     }
  26.     $updateAlarm.Name =  $newName
  27.     $updateAlarm.Enabled = $false
  28.     $updateAlarm.Description += ("`rOriginal name: " + $oldName)
  29.     $updateAlarm.Expression.Expression | %{
  30.       if($_.GetType().Name -eq "EventAlarmExpression"){
  31.         $_.Status = $null
  32.         $needsChange = $true
  33.       }
  34.     }
  35.  
  36.     $alarmObj.ReconfigureAlarm($updateAlarm)
  37.  
  38.     $alarmObj.Info.Name = $oldName
  39.     $alarmObj.Info.Enabled = $oldState
  40.     $alarmObj.Info.Description = $oldDescription
  41.   }
  42.  
  43.   $newAlarm = New-Object VMware.Vim.AlarmSpec
  44.   $newAlarm = $alarmObj.Info
  45.   $oldName = $alarmObj.Info.Name
  46.   $oldDescription = $alarmObj.Info.Description
  47.   foreach($destination in $To){
  48.     if($To.Count -gt 1){
  49.       $suffix = " (" + $destination.Name + ")"
  50.       if(($oldName.Length + $suffix.Length) -gt $alarmLength){
  51.         $newName = $oldName.Substring(0, $alarmLength - $suffix.Length) + $suffix
  52.       }
  53.       else{
  54.         $newName = $oldName + $suffix
  55.       }
  56.       $newAlarm.Name = $newName
  57.       $newAlarm.Description += ("`rOriginal name: " + $oldName)
  58.     }
  59.     $newAlarm.Expression.Expression | %{
  60.       if($_.GetType().Name -eq "EventAlarmExpression"){
  61.         $_.Status = $null
  62.         $needsChange = $true
  63.       }
  64.     }
  65.  
  66.     $newAlarm.Name = $oldName
  67.     $newAlarm.Description = $oldDescription
  68.     $alarmMgr.CreateAlarm($To.ExtensionData.MoRef,$newAlarm)
  69.   }
  70. }
  71.  
  72.  
  73. $to = Get-VM -Name "VM"
  74.  
  75.  
  76. $alarm = Get-AlarmDefinition -Name "Test1"
  77. Move-Alarm -Alarm $alarm -To $to -DeleteOriginal:$false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement