Advertisement
RJSN

XenDesktop - Restart disconnected desktops after 9 hours

Aug 12th, 2014
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #---------------------------------------------------------------------
  2. # Date         : 13-08-2013
  3. # Script name  : Force_restart.ps1
  4. # Description  : Restart disconnected desktops after 9 hours. Only desktops of a desktop group will be restarted.
  5. #                PowerOff disconnected desktops after 10 hours. Only desktops of a desktop group will be restarted.
  6. #                
  7. #                
  8. # Extra module : citrix*
  9. #        VMware.VimAutomation.Core
  10. #        VMware.VimAutomation.Vds
  11. #
  12. # Copyright    : ©2014 Login Consultants, all rights reserved.
  13. # History      : RJA 20140812 Initial version
  14. #                RJA 20140813 PowerOff added
  15. #---------------------------------------------------------------------
  16.  
  17. # Import the required Snap-ins
  18.  
  19. Add-PSSnapIn citrix*
  20. Add-PSSnapIn VMware.VimAutomation.Core
  21. Add-PSSnapIn VMware.VimAutomation.Vds
  22.  
  23.  
  24. # Query all  VDI desktops that are in maintenance mode longer then 9 hours.
  25.  
  26. $forcerebootmachines = Get-BrokerSession -SessionState Disconnected -Filter {SessionStateChangeTime -lt "-9:00"} -HypervisorConnectionName REPLACE WITH GROUP -DesktopGroupName "REPLACE WITH GROUP"| Select-Object HostedMachineName
  27.  
  28.  foreach ($item in $forcerebootmachines)
  29.  {
  30.     Restart-Computer -Force -computername $item.HostedMachineName
  31.  }
  32.  
  33.  
  34.  
  35. # Shutdown VDI desktops that do not respond to restart-computer
  36.  
  37. $ConfirmPreference="none"
  38. Set-PowerCliConfiguration -invalidcertificateaction Ignore
  39. Connect-VIServer REPLACE WITH VSPHERE SERVER -notdefault
  40.  
  41. $forcerebootmachines10 = Get-BrokerSession -SessionState Disconnected -Filter {SessionStateChangeTime -lt "-10:00"} -HypervisorConnectionName REPLACE WITH GROUP -DesktopGroupName "REPLACE WITH GROUP"| Select-Object HostedMachineName
  42.  
  43.  foreach ($item in $forcerebootmachines10)
  44.  {
  45.     Get-VM -Name $item.HostedMachineName | Stop-VM -Kill
  46.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement