Highlighted
Enthusiast
Enthusiast

Reset/Refresh Horizon Desktop in a Problem State using PowerShell

Jump to solution

The PowerShell interface for Horizon View is very confusing.  I want to automatically reset/refresh desktops that are in a problem state (Problem vCenter VMs in the View Admin website).  They tend to be either Already Used or Agent Unreachable. However, there seems to be no place to go to do the complete task. 

In the Horizon module available over PowerShell Repository, first of all, I have to download the HvHelper from Git before I can do anything easily.  I can use cmdlets like "Get-HVMachineSummary -State ALREADY_USED" to find machines in problem states, but I cannot find any command or cmdlet to reset or refresh them.

The View cmdlets from the vmware.view.broker snapin allow me to send a reset/ refresh/ recompose command, but "Get-DesktopVM" cannot find machines in a problem state.

How can I set it up to automatically reset or refresh desktop vms in a problem state?

1 Solution

Accepted Solutions
Highlighted
Hot Shot
Hot Shot

I have created a script and posted it on my github, I will probably do a blogpost about it next week Smiley Happy

Various_Scripts/remove_faulty_VDI_desktop.ps1 at master · Magneet/Various_Scripts · GitHub

This scripts asks for the connections erver, credentials after that it asks the state it wants to pull all desktops for that state (max results is 1000 at this point). Then it will ask about what desktop to remove and removes it. The error states are some of the most common ones but can be expanded easily.

remove_faulty_VDI_desktop.ps1.png

View solution in original post

7 Replies
Highlighted
Hot Shot
Hot Shot

The example script from the blog post announcing powercli 6.5 does just that. Only thing you might need to change is the restart-vmguest to reset or whatever you want.

Automating VMware Horizon 7 with VMware PowerCLI 6.5 | VMware End-User Computing Blog

Highlighted
Enthusiast
Enthusiast

Thank you, Magneet, for this information.  However, I tried it on my problem machines "Agent_Unreachable", and restarting the VM in vCenter didn't do the trick (which is what the script did).

I really need access to Horizon-View commands like Refresh and Remove that would not have a vCenter equivalent.  I suspect they would be found in the "View API", but it is a mystery to me.  I don't know how to take what I'm finding on the API website and translate it to PowerShell.

How do I Refresh or Remove problem vms in Horizon 7 using PowerShell?

Highlighted
Hot Shot
Hot Shot

There are multiple ways but delete desktop works better since refresh would be to nice and might fail if the agent refuses. I will get back to you about this, what would you prefer to get a list with desktops in a non-standard state and give you the option the remove the ones you want removed (my preference) or blindly delete them all (easier since I can largely re-use that example script above). They only issue I currently have is that get-hvmachine doesn't go above 1000 vdi's. I have created a workaround but that's not the best coding yet Smiley Happy

Highlighted
Enthusiast
Enthusiast

Thank you for offering to write a script for this, Magneet. I'm sure the whole community could benefit from this operation.

My thought is that the script searches for machines in a problem state, and then it Removes them.  My goal is to have this running without user intervention, as a scheduled task.  So that's your second option.

Thanks.

P.S. There are over 1000 desktops in my View environment.

Highlighted
Hot Shot
Hot Shot

I have created a script and posted it on my github, I will probably do a blogpost about it next week Smiley Happy

Various_Scripts/remove_faulty_VDI_desktop.ps1 at master · Magneet/Various_Scripts · GitHub

This scripts asks for the connections erver, credentials after that it asks the state it wants to pull all desktops for that state (max results is 1000 at this point). Then it will ask about what desktop to remove and removes it. The error states are some of the most common ones but can be expanded easily.

remove_faulty_VDI_desktop.ps1.png

View solution in original post

Highlighted
Enthusiast
Enthusiast

This is a fabulous script, and I have used it successfully in my environment.  Thank you, Magneet!

I just wanted to point out that this is where I was stuck and have now learned what to do in future scripts that involve the Horizon API. Here is a little of Magneet's code:

$spec = New-Object VMware.Hv.machinedeletespec

$spec.deleteFromDisk=$TRUE

AND

$services1.machine.machine_delete($removedesktop.id, $spec)

Thanks again!  Now if this could just come standard in Horizon PowerShell module!

Highlighted
Hot Shot
Hot Shot

I have to be honest that that $spec part has costed me at least 3 hours before i figured it out that it needed to be there even though it defaults to what I wanted already. The 1000 vm limit is really for the results part so you would need to have over 1000 vm's in that state before it hits that max and when you cleared those 1000 it will use the next 1000.

For a scheduled task you could get all the id's for the machines and use the machine_deletemachines method since that method should havent tested it yet accept multiple machine id's from an array. the hardest part for a scheduled task is almost the authentication part since the horizon part doesn't accept current credentials yet nor has it a hvcredentialstore item. For the Horizon vcheck I have used an encrypted file with only the password in it