PowerShell Commands to Verify the VDI Usage and Delete the Existing VDI from DDC Remotely

Rad DavidPrincipal Project Manager
CERTIFIED EXPERT
Published:
PowerShell is a very powerful scripting language that can help automate many aspects of maintaining and deploying a XenDesktop environment. To share some of my experiences creating PowerShell scripts, I’ve created this article that will give guidance and ideas for creating your own scripts for automating XenDesktop VDI usage and to delete the VDI if not used.

Why to Automate this?
 
First off, Why should you use Powershell rather than doing certain tasks manually? While automation might seem faster, time must be spent upfront developing the scripts. It is very easy to say it takes a few clicks to complete the process, when you have the automated script it just needs to you do a single click and the process is complete.

PowerShell Commands to Verify the VDI usage and Delete the Existing VDI from DDC Remotely :
The purpose of this article is to automate the process to verify the VDI usage and to delete the VDIs remotely using the PowerShell commands.

The below mentioned method does not require us to login to the server or even to the Desktop studio which helps us to save a lot time to delete the VDI.

This includes the following steps:
Step 1: First load the Citrix and VMware Snapins to run the commands using PowerShell. 
Note:The XenDesktop SDK comprises of a number of PowerShell snap-ins which are installed automatically by the XenDesktop installation wizard when you install either the controller or Desktop Studio components.
Step 2: Declare the common variables that are used in the script – like the Controller name, VDI Name and Desktop group name.
Step 3 : Verify the VDI usage.
Step 4: Inorder to delete the VDI we need to enable the maintenance mode of the VDI.
Step 5: Shutdown the VDI.
Step 6: Delete the VDI from the Desktop group.
Step 7: Delete the VDI from the Machine Catalog.

Step 1: PowerShel command to “Load the Citrix and VMWare Snapins”
 
Asnp Citrix.*
                      Add-PSSnapin VMware.VimAutomation.Core

Open in new window


Step 2: Declare the Common Variables:

$DDC = “DDC ServerName”
                      $VDI = “DomainName\VDI Name”
                      $DesktopGroup = “Desktop Group Name”

Open in new window

Step 3: Powershell Script to check the last connection time:
 
Asnp Citrix.*
                      $d = (Get-Date).AddDays(-60)
                      Get-BrokerDesktop -AdminAddress "$DDC" -DesktopGroupName "$DesktopGroup" -Filter { LastConnectionTime -le $d } | ft -a HostedMachineName,LastConnectionUser,LastConnectionTime,Protocol

Open in new window

Step 4: You need to enable maintenance mode of the VDI that you are planning to delete

Set-BrokerPrivateDesktop -AdminAddress "$DDC" -InMaintenanceMode $true -MachineName "$VDI"

Open in new window


Step 5: Shutdown the VM
This can be done using the PowerCli command or using the Citrix Powershell command

Using PowerCli command – VMWare

Get-VM ‘MachineName’| Shutdown-VMGuest -Confirm:$false

Open in new window


Using Citrix Powershell Command

New-BrokerHostingPowerAction -AdminAddress " $DDC" -MachineName " $VDI " -Action Shutdown

Open in new window

New-BrokerHostingPowerAction - Creates a new action in the power action queue.

Step 6: Delete the VDI from the Desktop Group 

Remove-BrokerMachine -AdminAddress "$DDC" -MachineName "$VDI” -DesktopGroup "$DesktopGroup" –Force

Open in new window


Step 7: Delete the VM from the Catalog

Remove-BrokerMachine -AdminAddress " $DDC " -MachineName "$VDI"

Open in new window

Remove-BrokerMachine  - Removes one or more machines from a desktop group.

Now we can combine all the codes and make it a script - save the file as filename.ps1
 
# Loading Snapins
                      Asnp Citrix.*
                      Add-PSSnapin VMware.VimAutomation.Core
                      	
                      #Declaring the Variables
                      $DDC = “DDC ServerName”
                      $VDI = “DomainName\VDI Name”
                      $DesktopGroup = “Desktop Group Name”
                      
                      # Enable Maintenace Mode
                      Set-BrokerPrivateDesktop -AdminAddress "$DDC" -InMaintenanceMode $true -MachineName    "$VDI"
                      
                      #Shutdown the VM
                      New-BrokerHostingPowerAction -AdminAddress " $DDC" -MachineName " $VDI " -Action Shutdown
                      
                      #Delete the VDI from the Desktop Group  
                      Remove-BrokerMachine -AdminAddress "$DDC" -MachineName "$VDI” -DesktopGroup "$DesktopGroup" –Force
                      
                      #Delete the VM from the Catalog
                      Remove-BrokerMachine -AdminAddress " $DDC " -MachineName "$VDI" 

Open in new window

Tested Environment
XenDesktop 5.6, PowerShell 3.0, Windows 7


 
3
5,235 Views
Rad DavidPrincipal Project Manager
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.