1 Reply Latest reply on Jul 21, 2020 12:14 PM by nburton935

    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