Force a restart if Disconnected session timer interval doesn’t work

In XenDesktop 5.6 there is a feature call: Disconnected session time interval which causes desktops to logoff after a specified amount of disconnect time. This is a nice feature but doesn’t always work well when there are applications that require user interaction at closure.

Often the result is that users return to their partly restarted desktop where some processes are closed and other are still open and this leads to failing applications.

 

Naamloos

The following script will query the Desktop Controller for sessions that are Disconnected for more then 9 hours and will force a logoff so the Desktop is rebooted. I scheduled this script to run every hour and never had a Desktop that was Disconnected more then 8 hours.

#———————————————————————

# Date : 13-08-2013
# Script name : Force_restart.ps1
# Description : Restart disconnected desktops after 9 hours. Only desktops of a desktop group will be restarted.
# PowerOff disconnected desktops after 10 hours. Only desktops of a desktop group will be restarted.
#
#
# Extra module : citrix*
# VMware.VimAutomation.Core
# VMware.VimAutomation.Vds
#
# Copyright : ©2014 Login Consultants, all rights reserved.
# History : RJA 20140812 Initial version
# RJA 20140813 PowerOff added
#———————————————————————

# Import the required Snap-ins

Add-PSSnapIn citrix*
Add-PSSnapIn VMware.VimAutomation.Core
Add-PSSnapIn VMware.VimAutomation.Vds
# Query all VDI desktops that are in maintenance mode longer then 9 hours.

$forcerebootmachines = Get-BrokerSession -SessionState Disconnected -Filter {SessionStateChangeTime -lt “-9:00”} -HypervisorConnectionName REPLACE WITH GROUP -DesktopGroupName “REPLACE WITH GROUP”| Select-Object HostedMachineName

foreach ($item in $forcerebootmachines)
{
Restart-Computer -Force -computername $item.HostedMachineName
}

# Shutdown VDI desktops that do not respond to restart-computer

$ConfirmPreference=”none”
Set-PowerCliConfiguration -invalidcertificateaction Ignore
Connect-VIServer REPLACE WITH VSPHERE SERVER -notdefault

$forcerebootmachines10 = Get-BrokerSession -SessionState Disconnected -Filter {SessionStateChangeTime -lt “-10:00”} -HypervisorConnectionName REPLACE WITH GROUP -DesktopGroupName “REPLACE WITH GROUP”| Select-Object HostedMachineName

foreach ($item in $forcerebootmachines10)
{
Get-VM -Name $item.HostedMachineName | Stop-VM -Kill
}

Use PastBin to copy the script without errors, script posted here: http://pastebin.com/MYpitCds

Advertisements
This entry was posted in Citrix XenDesktop and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s