-
New PowerShell Script: Remove-HostingConnection V1.0
November 2, 2017
Citrix Virtual Apps and Desktops, PowerShell, XenApp/XenDesktop 7.0 - 7.7
You have moved to a new/updated/upgraded hypervisor or added new/updated/upgraded storage. Then you go to delete either the entire old hosting connection or just an old resource connection and received the dreaded popup:
“There is currently an active background action”
ARGH!!! What to do?
Previously, I wrote about my issues with removing a hosting connection because of a mysterious active background task. In that article, I stated, “If this were a common occurrence, I would write a script to do all this…”. Much to my dismay, this has become a common occurrence with you and your customers. It was time to create a script to automate this tedious process.
This is the first script I am creating that makes changes to a XenDesktop Site. Because of this, I try to do as much error checking as possible and stop the script from running if I find anything even slightly wrong. There are several places where I have code similar to:
If(Some Condition) { #we should never get here Write-Host "Unable to <do some process>. You shouldn't be here! Script will close." Write-Host "If you get this message, please email webster@carlwebster.com" -ForegroundColor Red $error Exit }
If there are no active tasks for the hosting connection selected, then NOTHING is removed from the Site. The script will state there were no active tasks found and end.
Here are examples of hosting and resource connections and the script in action.
I also added support for -WhatIf and -Confirm thanks to the clear and simple articles from Adam Bertram.
This will allow you to be safe and verify the script’s actions.
You can always find the most current script by going to https://www.carlwebster.com/where-to-get-copies-of-the-documentation-scripts/
Thanks
Webster
5 Responses to “New PowerShell Script: Remove-HostingConnection V1.0”
Leave a Reply
February 17, 2019 at 5:21 am
Ok… I have a pesky controller in my environment that won’t let go of Nutanix after I upgraded the hypervisor. I’ve removed the machine catalogs and there are still 13 background jobs pending. Now, I have looked into clearing these jobs, but there are a lot to sift through. Like over 10000 kind of a lot. Your script cannot handle that too easily apparently. I am not sure what needs to be changed with the script so I figured I would nudge you and see if you’ve heard of this one before. Code looks pretty tight, btw.
February 18, 2019 at 2:29 pm
I have not heard of that before and without having access to your system, I don’t know what you need to do to resolve it. Are you able to open a support ticket with either Nutanix or Citrix? I have a few friends who work at Nutanix. I will see if they have any suggestions.
Thanks
Webster
November 2, 2017 at 5:23 am
Nice script Carl – can’t test it, but I think I spotted a slight logic flaw?
Your ShouldProcess will rightly only operate if -WhatIf isn’t provided – but it looks like you’re going to write fails to your log if you supply WhatIf. Which isn’t really a fail – you just didn’t carry out the action, in which case no point in logging?
If I’m reading it right, here’s a typical sample with line numbers where I think the issue is:
552: Set Succeeded = false
555: Start the highlevel log action with Start-LogHighLevelOperation
563: remove-item with whatif…
569: and if that worked, succeeded = true
But we passed -whatif, so succeeded = false – for something we never tried to do
583: close and write a log for that fail
Would suggest perhaps wrapping the logging elements as well in the ShouldProcess blocks. Otherwise, every time you pass WhatIf (testing etc), you’ll be logging fails all over the place.
November 2, 2017 at 5:27 am
When I get back home and to my lab, I will re-test that scenario. Thanks for looking at the script.
Webster
November 4, 2017 at 3:56 pm
I found the logic issue you reported. Thanks for finding the bug. I have submitted the fix to my PowerShell mentor for his feedback. It appears to work for me. If you would like to test the fix, just send me an email. webster@carlwebster.com
Thanks
Webster