2 Replies Latest reply on Sep 23, 2020 10:29 AM by Qander

    Automatic cleanup of machines in 7.11.0

    Qander Lurker

      Hello there,

       

      I'm looking for a way to determine if a machine hasn't been logged on for X amount of days.

      When the user hasn't logged on to it for more then X amount of days the machine may be automatically deleted.

       

      The users shouldn't lose his entitlement to the pool.

       

      This way we will be able to cleanup inactive machines and regain CPU, ram and storage for these machines automatically.

       

      It will be really appreciated if someone could help me out in this one.

       

      Thank you,

       

      Remie

        • 1. Re: Automatic cleanup of machines in 7.11.0
          nburton935 Enthusiast

          It would take some scripting, but you can likely get it done.

           

          If your VMs are within a specific OU, you can get a list of the machines using Get-ADComputer -Filter * -SearchBase 'OU=ouname,DC=yourdomain,DC=com' -Properties * | sort lastlogondate | select name,lastlogondate

           

          That will at least get you a list of your machines and their last logons. If you're wanting to fully automate it, you can apply a filter based on your date requirements, pipe those names to the Remove-HVMachine commandlet, and have it run as a scheduled task. Here's the get-help for Remove-HVMachine (part of PowerCLI).

           

          NAME

              Remove-HVMachine

           

           

          SYNOPSIS

              Remove a Horizon View desktop or desktops.

           

           

           

           

          SYNTAX

              Remove-HVMachine [-MachineNames] <Array> [-DeleteFromDisk] [[-HVServer] <Object>] [-WhatIf] [-Confirm]

              [<CommonParameters>]

           

           

           

           

          DESCRIPTION

              Deletes a VM or an array of VM's from Horizon. Utilizes an Or query filter to match machine names.

           

           

           

           

          PARAMETERS

              -MachineNames <Array>

                  The name or names of the machine(s) to be deleted. Accepts a single VM or an array of VM names.This is a

                  mandatory parameter.

           

           

                  Required?                    true

                  Position?                    1

                  Default value

                  Accept pipeline input?       false

                  Accept wildcard characters?  false

           

           

              -DeleteFromDisk [<SwitchParameter>]

                  Determines whether the Machine VM should be deleted from vCenter Server. This is only applicable for managed

                  machines.

                  This must always be true for machines in linked and instant clone desktops.

                  This defaults to true for linked and instant clone machines and false for all other types.

           

           

                  Required?                    false

                  Position?                    named

                  Default value                False

                  Accept pipeline input?       false

                  Accept wildcard characters?  false

           

           

              -HVServer <Object>

                  The Horizon server where the machine to be deleted resides. Parameter is not mandatory,

                        but if you do not specify the server, than make sure you are connected to a Horizon server

                        first with connect-hvserver.

           

           

                  Required?                    false

                  Position?                    2

                  Default value

                  Accept pipeline input?       false

                  Accept wildcard characters?  false

           

           

              -WhatIf [<SwitchParameter>]

           

           

                  Required?                    false

                  Position?                    named

                  Default value

                  Accept pipeline input?       false

                  Accept wildcard characters?  false

           

           

              -Confirm [<SwitchParameter>]

           

           

                  Required?                    false

                  Position?                    named

                  Default value

                  Accept pipeline input?       false

                  Accept wildcard characters?  false

           

              -------------------------- EXAMPLE 1 --------------------------

           

              PS C:\>Remove-HVMachine -HVServer 'horizonserver123' -MachineNames 'LAX-WIN10-002'

           

              Deletes VM 'LAX-WIN10-002' from HV Server 'horizonserver123'

           

              -------------------------- EXAMPLE 2 --------------------------

           

           

              PS C:\>Remove-HVMachine -HVServer 'horizonserver123' -MachineNames $machines

           

           

              Deletes VM's contained within an array of machine names from HV Server 'horizonserver123'

           

              -------------------------- EXAMPLE 3 --------------------------

           

           

              PS C:\>Remove-HVMachine -HVServer 'horizonserver123' -MachineNames 'ManualVM01' -DeleteFromDisk:$false

           

           

              Deletes VM 'ManualVM01' from Horizon inventory, but not from vSphere. Note this only works for Full Clone VMs.

           

          -Nick

          1 person found this helpful
          • 2. Re: Automatic cleanup of machines in 7.11.0
            Qander Lurker

            Hi Nburton935,

             

            Thanks for pointing me the remove-hvmachine command.

             

             

            I have made a script which doesn't work for some reason and I cannot understand what I'm doing wrong.

            Here is the code:

             

            $HorizonServer = "horizonserver.domain.com"
            $dagen = (Get-Date).AddDays(-31) 
            $vms = Get-ADComputer -Filter 'Description -LIKE "*VMWARE*" -AND lastLogonDate -lt $dagen' -SearchBase 'OU=VDI OU,DC=Domain,DC=com' -Properties * | select name
            
            
            Connect-HVServer -server $HorizonServer -user domain\someserviceuser -password MyIncrediblyHardPassword
            foreach($vm in $vms) 
            {
            Remove-HVMachine -machinenames $vm -deletefromdisk -confirm
            }
            Disconnect-HVServer -Confirm
            

             

            When I run the command as domain\someserviceuser manually with a single VDI machine pointed in the remove-hvmachine command it works just fine.

             

            PS C:\Users\administrator.PRIMELINE> foreach($vm in $vms)

            {

            Remove-HVMachine -machinenames $vm -deletefromdisk -confirm

            }

            Exception calling "Machine_GetInfos" with "2" argument(s): "There is an error in the XML document."

            At C:\Program Files\WindowsPowerShell\Modules\VMware.Hv.Helper\VMware.HV.Helper.psm1:10536 char:7

            +       $deleteMachine = $machineService.Machine_GetInfos($services,$de ...

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

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

                + FullyQualifiedErrorId : InvalidOperationException

             

            Attempting log off of machines

            Attempting to Delete:

             

             

            Exception calling "Machine_DeleteMachines" with "3" argument(s): "There is an error in the XML document."

            At C:\Program Files\WindowsPowerShell\Modules\VMware.Hv.Helper\VMware.HV.Helper.psm1:10602 char:7

            +       $machineService.Machine_DeleteMachines($services,$deleteMachine ...

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

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

                + FullyQualifiedErrorId : InvalidOperationException

             

            Exception calling "Machine_GetInfos" with "2" argument(s): "There is an error in the XML document."

            At C:\Program Files\WindowsPowerShell\Modules\VMware.Hv.Helper\VMware.HV.Helper.psm1:10536 char:7

            +       $deleteMachine = $machineService.Machine_GetInfos($services,$de ...

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

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

                + FullyQualifiedErrorId : InvalidOperationException

             

             

            What am I doing wrong here?