1. ## =====================================================================
  2. ## Title : Shutdown running VM's
  3. ## Description : Script to shutdown all runnning guests in a virtual infrastructure
  4. ## Author : Marco Baars
  5. ## January 2010
  6. ## =====================================================================
  7.  
  8. add-pssnapin VMware.VimAutomation.Core
  9.  
  10. # Connect to Virtual Center server
  11. $VIserver = "viserver"
  12. Connect-VIServer $VIserver
  13.  
  14.  
  15. # Run trough all guests
  16. $VMarray = Get-VM
  17. cls
  18. foreach ($item in $VMarray)
  19. {
  20. if ($Item.Powerstate -eq "Poweredon")
  21. {
  22. $ServerName = $Item.Name
  23. "Server " + $ServerName + " is powered on"
  24. "Shutting down " + $ServerName
  25.  
  26. # The VMware way (simulates power button)
  27. # Get-VM $Item.Name | Shutdown-VMGuest -Confirm
  28.  
  29. # This one suspends the virtual machine
  30. # Get-VM $Item.Name | Suspend-VMGuest
  31.  
  32. # Shutdown the Windows way via WMI
  33. $win32OS = get-wmiobject win32_operatingsystem -computername $ServerName
  34. $win32OS.psbase.Scope.Options.EnablePrivileges = $true
  35. $win32OS.win32shutdown(1)
  36. }
  37.  
  38. }
  39.  
  40.  

Last Updated (Monday, 18 January 2010 16:30)