VMware Horizon Community
visan35
Enthusiast
Enthusiast
Jump to solution

PowerCLI Script to unassign/remove the unused VDI VMs.

Can someone please help to automate below task:

I am using linked clone and full clone VDI pool. A VDI is persistently assigned to the user's on first logon. When the user's leave the company the AD user account is disabled or deleted but the desktops are still assigned to this user account.  I would like to run a script once in a week that can find and remove the VM of the user's that don't work in company anymore. I like to do be below:

1. Compare the list of AD accounts and the VDI users.

2. Generate list of VDI VM's and user's assigned which can be removed from the VDI pool and the VCenter.

3. Remove the VDI VM permanently

So far I am able to generate the list of user names and VDI VM's that can be removed.

But don't know how to remove or unassigned the user from the connection server.

Visan

1 Solution

Accepted Solutions
Magneet
Hot Shot
Hot Shot
Jump to solution

for linked clones & full clones: $services1.machine.machine_delete((get-hvmachine -machinename MACHINENAME).id, $spec)

$spec doesn't need to be defined at all, just put it in there

for instant clones this works: $services1.machine.machine_recover((get-hvmachine -machinename Pool05-001).id)    the reset-hvmachine that was added this week to vmware.hv.helper does the same trick

View solution in original post

7 Replies
Magneet
Hot Shot
Hot Shot
Jump to solution

removing the machine I have described in this blog post: https://www.retouw.nl/powercli/adding-manual-desktops-in-horizon-view-and-assigning-them-using-power...

Unassigning it is a bit more difficult since this is an advanced settting that can't be set when it's empty or maybe it works with $null but don't think so (haven't tried yet). For removing the machine it doesn't need to be unassigned so what I would recommend is forget about unassigning and just remove it.

visan35
Enthusiast
Enthusiast
Jump to solution

Magneet

Thanks for your quick reply. The desktop pool type is "Automated Desktop Pool"

Can we remove the VM without querying the desktop pool id? I am able to create list of user name and the vm name to delete.

I get below message.

c:\$services1.Desktop.Desktop_RemoveMachinesFromManualDesktop((get-hvpool -poolname testpool02).id,(Get-HVMachine -MachineName tpool02-003).id)

Exception calling "Desktop_RemoveMachinesFromManualDesktop" with "2" argument(s): "ExceptionType : VMware.Hv.InvalidArgument

ErrorMessage : Desktop should be manual type

ParameterName : id"

At line:2 char:1

+ $services1.Desktop.Desktop_RemoveMachinesFromManualDesktop((get-hvpoo ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

0 Kudos
Magneet
Hot Shot
Hot Shot
Jump to solution

ah that's a different one indeed, mine's for a manual pool, I think you would need to use Machine_Delete then but I will test this for you tonight and will probably end up building remove-hvmachine in vmware.hv.helper :smileygrin:

Magneet
Hot Shot
Hot Shot
Jump to solution

for linked clones & full clones: $services1.machine.machine_delete((get-hvmachine -machinename MACHINENAME).id, $spec)

$spec doesn't need to be defined at all, just put it in there

for instant clones this works: $services1.machine.machine_recover((get-hvmachine -machinename Pool05-001).id)    the reset-hvmachine that was added this week to vmware.hv.helper does the same trick

visan35
Enthusiast
Enthusiast
Jump to solution

Hi Magneet

Your command works great. It's just the vdi VM must be in powered on state and available. If thetvm is powered off or in a disconnected state than it gives erroe. But its not so big issue as I can power cycle the VMS before deleting them.

Thanks for your quick response.

Visan

0 Kudos
Magneet
Hot Shot
Hot Shot
Jump to solution

very good, will test it as well when I have time with powered off or disconnected vm's because afaik it can be done then in the gui (was this about linked clones??)

0 Kudos
mdoleh-CD
Contributor
Contributor
Jump to solution

I know this is an older post, but do you mind sharing your script. I'm trying to accomplish the same thing. We are running VMware Horizon 8.11.

1. Compare the list of disabled/ deleted AD accounts and the VDI users.

2. Generate list of VDIs and users assigned which can be removed from the Horizon and the vCenter.

3. Remove the VDI permanently

0 Kudos