by Marco Hofmann, CTA
Today in Citrix Studio, I discovered a dead Hosting connection that I had set up years ago for some MCS tests. When I tried to simply delete the connection, I received the following message:

The error message leads to the following (helpful!) KB article: CTX138318
After following the different steps outlined in the article…
asnp citrix* Cd XDHyp:\HostingUnits Get-ChildItem Get-ProvTask | Where-Object { $_.ImagesToDelete | Where-Object { $_.HostingUnit -eq "f4dcd282-3d9b-478a-9107-2cafe6f6f124" } }
… I ended up with something like this:
TaskId : db2ed90d-9f5f-43c9-8ee0-01dfde159544 Active : False Host : XA-WMS2016-01 DateStarted : 28.05.2018 22:47:27 Metadata : {} CurrentOperation : TaskProgress : 100 LastUpdateTime : 28.05.2018 22:47:37 ActiveElapsedTime : 9 DateFinished : 28.05.2018 22:47:37 TerminatingError : Faulted Type : DisusedImageCleanUp Status : Finished ImagesToDelete : {HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_18] MCS-2012R2-baseDisk-datastore-47764/12 Feb 2017 19-53-35.4705Z.vmdk,resgroup-8,Vmfs : StorageId datastore-47764', Last Processed '5/28/2018 6:59:24 PM', HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_14] MCS-2012R2-baseDisk-datastore-47360/MCS-2012R2-baseDisk-datastore-47360.vmdk,resgroup-8,Vmfs : StorageId datastore-47360', Last Processed '5/28/2018 7:02:31 PM'} HeartbeatInterval : 01:00:00 RetryInterval : 06:00:00 WorkflowStatus : Terminated TaskExpectedCompletion : TaskId : 10f7b103-0675-42c8-9a97-0c20016af145 Active : False Host : XA-WMS2016-02 DateStarted : 14.04.2018 21:10:39 Metadata : {} CurrentOperation : TaskProgress : 100 LastUpdateTime : 17.06.2018 02:59:26 ActiveElapsedTime : 5464104 DateFinished : 17.06.2018 02:59:26 TerminatingError : Faulted Type : DisusedImageCleanUp Status : Finished ImagesToDelete : {HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_18] MCS-2012R2-baseDisk-datastore-47764/12 Feb 2017 19-53-35.4705Z.vmdk,resgroup-8,Vmfs : StorageId datastore-47764', Last Processed '6/16/2018 11:01:05 PM', HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_14] MCS-2012R2-baseDisk-datastore-47360/MCS-2012R2-baseDisk-datastore-47360.vmdk,resgroup-8,Vmfs : StorageId datastore-47360', Last Processed '6/16/2018 11:04:12 PM'} HeartbeatInterval : 01:00:00 RetryInterval : 06:00:00 WorkflowStatus : Terminated TaskExpectedCompletion : TaskId : 0d3bbe35-581d-46c8-b85f-23aa9911ed6f Active : True Host : XA-WMS2016-01 DateStarted : 17.06.2018 02:59:10 Metadata : {} CurrentOperation : Running TaskProgress : LastUpdateTime : 18.06.2018 17:05:10 ActiveElapsedTime : 137155 DateFinished : TerminatingError : Type : DisusedImageCleanUp Status : Running ImagesToDelete : {HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_18] MCS-2012R2-baseDisk-datastore-47764/12 Feb 2017 19-53-35.4705Z.vmdk,resgroup-8,Vmfs : StorageId datastore-47764', Last Processed '6/18/2018 12:02:03 PM', HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_14] MCS-2012R2-baseDisk-datastore-47360/MCS-2012R2-baseDisk-datastore-47360.vmdk,resgroup-8,Vmfs : StorageId datastore-47360', Last Processed '6/18/2018 12:05:09 PM'} HeartbeatInterval : 01:00:00 RetryInterval : 06:00:00 WorkflowStatus : Running TaskExpectedCompletion : TaskId : 210c7daf-ed26-4275-8cab-26dee5f941e7 Active : False Host : XA-WMS2016-01 DateStarted : 15.04.2018 11:20:05 Metadata : {} CurrentOperation : TaskProgress : 100 LastUpdateTime : 15.04.2018 11:20:35 ActiveElapsedTime : 29 DateFinished : 15.04.2018 11:20:35 TerminatingError : Faulted Type : DisusedImageCleanUp Status : Finished ImagesToDelete : {HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_18] MCS-2012R2-baseDisk-datastore-47764/12 Feb 2017 19-53-35.4705Z.vmdk,resgroup-8,Vmfs : StorageId datastore-47764', Last Processed '4/15/2018 9:14:32 AM', HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_14] MCS-2012R2-baseDisk-datastore-47360/MCS-2012R2-baseDisk-datastore-47360.vmdk,resgroup-8,Vmfs : StorageId datastore-47360', Last Processed '4/15/2018 9:15:05 AM'} HeartbeatInterval : 01:00:00 RetryInterval : 06:00:00 WorkflowStatus : Terminated TaskExpectedCompletion : TaskId : 458a44a9-214d-4d2e-80d1-400b6c81dd3b Active : False Host : XA-WMS2016-01 DateStarted : 20.05.2018 03:08:58 Metadata : {} CurrentOperation : TaskProgress : 100 LastUpdateTime : 20.05.2018 03:09:04 ActiveElapsedTime : 9 DateFinished : 20.05.2018 03:09:04 TerminatingError : Faulted Type : DisusedImageCleanUp Status : Finished ImagesToDelete : {HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_18] MCS-2012R2-baseDisk-datastore-47764/12 Feb 2017 19-53-35.4705Z.vmdk,resgroup-8,Vmfs : StorageId datastore-47764', Last Processed '5/20/2018 2:55:10 AM', HostingUnitUid 'f4dcd282-3d9b-478a-9107-2cafe6f6f124', Image 'DiskId Version=2,LsiLogicScsi,1000,group-v47771,[v3700_p01_14] MCS-2012R2-baseDisk-datastore-47360/MCS-2012R2-baseDisk-datastore-47360.vmdk,resgroup-8,Vmfs : StorageId datastore-47360', Last Processed '5/20/2018 3:01:05 AM'} HeartbeatInterval : 01:00:00 RetryInterval : 06:00:00 WorkflowStatus : Terminated TaskExpectedCompletion :
The list is normally 5 times this long!
Which, I should process via:
Remove-ProvTask -TaskID
Which, I didn’t want to do manually for each entry. But I remembered the session from Andreas Nick at the TecCon2018, where he showed examples about how to pipe the output of a command to a select grid. I’m not good at PowerShell at all, but I wanted to try it anyway. After two hours of straight web research, I ended up with the following code, which simplified this process a lot!
asnp citrix* Cd XDHyp:\HostingUnits $Hosting = Get-ChildItem | Out-GridView -PassThru -Title "Select Hosting Connection" | Select-Object -Property HostingUnitUid Get-ProvTask | Where-Object { $_.ImagesToDelete | Where-Object { $_.HostingUnit -eq $Hosting.HostingUnitUid } } | Out-GridView -PassThru -Title "Select Provisioning Tasks for removal" | ForEach-Object { Remove-ProvTask -TaskID $_.TaskId }

To explain each step in detail:
Line 1 | Make PowerShell aware of the Citrix cmdlets. |
Line 2 | Switch to the Hypervisor Hosting connections of XenDesktop. |
Line 3 | Write the HostingUnitUid of the affected connection into a variable after a GridView Select. |
Line 4 | Create a list of all pending tasks and … |
Line 5 | Present it in a second GridView Select so you could delete all or selected tasks. The selection is being piped to the Remove command. |
This will look similar to this:


In the Out-GridView, you will first select the affected Hosting connection and in the second window, you will select all ProvTasks you may want to remove. Most of the time this will be all. There are certainly better ways to solve this, but the intent of this post is just to show you with how little work it is to optimize a repetitive task with simple PowerShell tricks.
Sometimes it can happen, that a ProvTask is “Running” and you can’t remove it. For this case, I found a hint at discussions.citrix.com linked at the bottom.
This worked great for me, but you should test this first!
Source: https://support.citrix.com/article/CTX138318
https://discussions.citrix.com/topic/333521-cant-delete-hosting-connection/