Remotely clean up Virtual Machines drives - XenDesktop

· 2 min read

Following up the previous blogs XenDesktop XenApp 7.x – vmware / ad / delivery group notes and descriptions sync and Expand virtual machines hard disk – automation and continue in automated task, I had to clean up the D: drive of different XenDesktop Delivery Group. As there was no security restriction on the D: drive some users used it as a repository for some of their project… That caused some issues :

  • Users complain of losing their working data from a session to another (pooled VDI, new logon = new vm)
  • Some disk space notification where displayed to random users…
  • Calls where raise to the helpdesk support team

Beside hiding the D: drive to avoid non necessary access (ie : non system access) check this blog to do so : Citrix XenApp – Hiding system drives part 1/2 an automated task had to be performed to “clean” this D: drive

The variable $XDDC is the FQDN of a Delivery Controler, $Exclusion is the files and folder you want to exclude from being removed.

For example : the directories “logs” “pvsvm” “System Volume Information” “$RECYCLE.BIN” and the files “dedicateddumpfile.sys” “pagefile.sys” and “vdiskdif.vhdx” will be ignore from the delete process. Most of these files and directory are system protected anyway it’s more to avoir error during script execution.

Once you have a clear list of what you need and want to keep you can proceed to the next step.

 

Add-PSSnapin citrix*
 
$XDDC = "XDDCFQDN:80"
$Exclusion = "logs", "pvsvm", "System Volume Information", "$RECYCLE.BIN", "dedicateddumpfile.sys", "pagefile.sys", "vdiskdif.vhdx"
 
foreach ($list in (Get-BrokerMachine  -AdminAddress $XDDC -Filter "((SessionSupport -eq `"SingleSession`" -and DesktopGroupName -eq `"VDI`"))" -Skip 0 | Select-Object HostedMachineName)) {
 
$list.HostedMachineName = $list.HostedMachineName.Insert(0,'')
$list.HostedMachineName += "d$"
 
# Write-Host $list.HostedMachineName
 
 
   Get-ChildItem -Path $list.HostedMachineName -Exclude $Exclusion | foreach ($_) {
       "CLEANING :" + $_.fullname
       Remove-Item $_.fullname -Force -Recurse -Verbose
       "CLEANED... :" + $_.fullname 
 
  } 
 
}

This script will clean everything which is not in the $Exclusion list so be careful when you run the script. This script assume all the targeted VM are switched ON of course.

Leave a comment bellow if you have an idea how to improve this script !