Wednesday, April 11, 2018

How to remove orphaned Horizon View VMs

Save the commands outlined in red into a .ps1 script file. 

Run the script using PowerCLI from the vCenter server. In case of an appliance, run it from any machine that has PowerCLI installed.

$vcserver = "vcenter.domain.com"
$vcusername = "vcenter_admin"
$vcpassword = "password"

Connect-VIServer -Server $vcserver -User $vcusername -Password $vcpassword

$allVMs=Get-VM
foreach ($vm in $allVMs) { if ($vm.ExtensionData.Runtime.ConnectionState -eq "orphaned") {$vm | Remove-VM}
}

If you get an error about thRemove-VM method being disabled by horizon.daas, you will first need to unprotect folders in vCenter. To do that run the following command using PowerCLI on the Horizon Connection Broker.

IcUnprotect.cmd -vc hostname_or_IP_address -uid user_ID -password password [-clusterId cluster_ID] [-includeFolders]

Once folders have been unprotected, re-run the original script.