Wednesday, July 16, 2014

Programmatically Detecting when a VM Changes State

Nice approach can be found here.
# WMI Query that specifies what events we will watch for
$Query = "Select * from __InstanceModificationEvent within 3 where TargetInstance ISA 'MSVM_ComputerSystem' `
          and TargetInstance.EnabledState <> PreviousInstance.EnabledState"
 
# Script block that will run whenever the event fires
[ScriptBlock]$Action = {
   $VMName = $Event.SourceEventArgs.NewEvent.TargetInstance.ElementName
 
   switch ($Event.SourceEventArgs.NewEvent.TargetInstance.EnabledState)
      {
        2 {$vmState = "running"}
        3 {$vmState = "turned off"}
        9 {$vmState = "paused"}
        6 {$vmState = "in a saved state"}
        10 {$vmState = "starting"}
        4 {$vmState = "stopping"}
        default {$vmState = "in an unknown state..."}
       }
 
   if ($Event.SourceEventArgs.NewEvent.TargetInstance.EnabledState -eq 1)
      {$vmState = $Event.SourceEventArgs.NewEvent.TargetInstance.OtherEnabledState}
 
   write-host "The virtual machine '$($vmName)' is now $($vmState)."}
 
# Register for the events
Register-WMIEvent -Query $Query -Action $Action -Namespace root\virtualization\v2
 
# To clean up - run "Get-EventSubscriber | Unregister-Event"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.