• 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 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.

    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://carlwebster.com/where-to-get-copies-of-the-documentation-scripts/

    Thanks

    Webster

    About Carl Webster

    Webster is a Sr. Solutions Architect for Choice Solutions, LLC and specializes in Citrix, Active Directory and Technical Documentation. Webster has been working with Citrix products for many years starting with Multi-User OS/2 in 1990.

    View all posts by Carl Webster

    3 Responses to “New PowerShell Script: Remove-HostingConnection V1.0”

    1. Andrew Williamson Says:

      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.

      Reply

      • Carl Webster Says:

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

        Webster

        Reply

      • Carl Webster Says:

        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

        Reply

    Leave a Reply