New PowerShell Script: Remove-HostingConnection V1.0

New PowerShell Script: Remove-HostingConnection V1.0

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:

1
2
3
4
5
6
7
8
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.

Figure 1
Figure 1
Figure 2
Figure 2
Figure 3
Figure 3
Figure 4
Figure 4
Figure 5
Figure 5
Figure 6
Figure 6
Figure 7
Figure 7
Figure 8
Figure 8
Figure 9
Figure 9
Figure 10
Figure 10
Figure 11
Figure 11
Figure 12
Figure 12
Figure 13
Figure 13

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.

Figure 14
Figure 14

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 Comments

  1. Jonathan Redford

    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.

    • Carl Webster

      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

  2. Andrew Williamson

    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.

    • Carl Webster

      When I get back home and to my lab, I will re-test that scenario. Thanks for looking at the script.

      Webster

    • Carl Webster

      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

Comments are closed