VMware Horizon Community
rseekell
Enthusiast
Enthusiast
Jump to solution

Reset/Refresh Horizon Desktop in a Problem State using PowerShell

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
Magneet
Hot Shot
Hot Shot
Jump to solution

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

9 Replies
Magneet
Hot Shot
Hot Shot
Jump to solution

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

rseekell
Enthusiast
Enthusiast
Jump to solution

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?

Magneet
Hot Shot
Hot Shot
Jump to solution

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

rseekell
Enthusiast
Enthusiast
Jump to solution

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.

Magneet
Hot Shot
Hot Shot
Jump to solution

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

rseekell
Enthusiast
Enthusiast
Jump to solution

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!

Magneet
Hot Shot
Hot Shot
Jump to solution

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

lurban_ITD
Contributor
Contributor
Jump to solution

 

Spoiler
Wouldn't this still leave the vm in an error state in view?



shouldn't it be "Recovered" instead? 

using the api Machine_Recover?

 

I just used the script, it deletes the VM, View will re-create it (settings in my setup) but the problem that occurs is when a vm is deleted and not recovered it looses it's assignment.

 

if you ise the view admin site, you chose recover, it deleted the vm in a way that it still retains its user assignments. Maybe the Machine_Recover will do it properly? I don't know how to use the API 😞

 

Reply
0 Kudos
lurban_ITD
Contributor
Contributor
Jump to solution

I figured it out:

 


$Services1.Machine.Machine_Recover($VM.Id)

Reply
0 Kudos