PowerShell Script: Decommision VDI (XenDesktop & vCentre)

I have been trying to learn PowerShell and this is my first attempt at a script that just works. This PowerShell script will put a XenDesktop VDI into maintenance mode, disable the object in Active Directory and shutdown the VM in vCentre as well as moving between containers (if required). This is handy if you have a lot of VDIs that need to decommissioned.

See below for the script, you will need to ensure a list of names are in a CSV called IMPORT_DecomissionVDI.csv. The reason for the ‘IMPORT’ is I find it easier to keep track of what a CSV does, so ‘EXPORT’ / ‘IMPORT’ etc. The same goes for the PowerShell script files ‘Set’ if it’s going to change something and ‘Get’ if it’s going to return information. Handy so you don’t run the wrong script!!

An example of what the CSV should look like:

PS-VDIName

Don’t forget to change the variables at the start.

#========================================================================
# File: Set-DecommisionVDI.ps1
#
# Description: This script imports a list of VDIs to be disabled, sets the VDI in XenDesktop in maintenance mode, then disables the AD account 
# and moves to an OU if required. Lastly the script moves the VDI in vCentre if required and shuts down the VDI.
#
# Date: 12/09/14 11.52 PM
# Author: Chris Stark
# Version 0.1
# Blog: ChrisStark.co
# Twitter: twitter.com/ChrisLStark
# Google+: http://google.com/+ChrisStark86
# LinkedIn: http://www.linkedin.com/profile/view?id=clstarks
# Link: http://chrisstark.co/2014/09/powershell-script-decommision-vdi-xendesktop-vcentre/
#========================================================================

# Imports Citrix and VMware PowerShell snapins

Add-PSSnapin Citrix.*
Add-PSSnapin VMware.VimAutomation.Core

# Variables for Citrix XenDesktop Server, vCentre, Domain and paths to move VDIs

$Domain = ' '
$XenDesktop = ' '
$VCentre = ' '
$CSVFile = 'IMPORT_DisableVDIs.csv'
$DisableADPath = 'OU=Disabled Computer Accounts,DC=DOAMIN,DC=co,DC=uk'
$VcentreFolder = "Disabled VDIs"

# Connects to the Citrix XenDesktop server

Get-BrokerSite -AdminAddress $XenDesktop

# Connects to VMware vCentre

Connect-VIServer $VCentre

# Imports the list of computers to disable

$CSV = import-csv $CSVFile

# Loops through the VDIs

foreach ($Item in $CSV)
{
# Sets the variable VDIName to the name of the VDI being disabled

 $VDIName = $Domain+$Item.VDIName

# Sets the VDI to maintenance mode 

 Get-BrokerDesktop -MachineName $VDIName | Set-BrokerPrivateDesktop -InMaintenanceMode $true

# Disables the AD account of the VDI

 Get-ADComputer $VDIName | Set-ADComputer -Enabled $false

# Moves the VDI to the disabled OU - remove line if not required

 Get-ADComputer $VDIName | Move-ADObject -TargetPath $DisableADPath

# Moves the VM to the Disabled VDIs folder - remove line if not required

 Move-VM -VM $VDIName -Destination $VcentreFolder

# Shuts down the VDI

 Get-VM $VDIName | Shutdown-VMGuest -Confirm:$false
}

Download here

Citrix XenDesktop + Default printers not sticking

I had an issue whereby default printers were not sticking. Every time a user set a default printer and then re-logged in the printer would revert back.

Took me a while to find the cause, as I was either RDPing or using the vSphere client to troubleshoot and not via the Citrix client.

Turns out, there’s a policy (bear in mind I’m new to Citrix) that allows Citrix XenDesktop to control default printers. Turned this off, and BOOM! the default printers stayed.

To change the policy, open Desktop Studio.

Navigate to the users policy:

XenPrinter1

Within the Users policies you will see the policies you’ve. In this example mine is called “Unfiltered” select this and then Edit.

XenPrinter2

Click on settings and then scroll to “Printing” on the right next to Default printer click add.XenPrinter4Here is the default policy:

XenPrinter6

We don’t want Citrix to change the printer therefore change to “Do not adjust the users default printer”.

XenPrinter7

Click ok. Citrix will let you know its updating the policy.

XenPrinter8

You can double check the policy is applied by editing and verifying it has been added.

XenPrinter9

Success!