Collect or delete VMs that are older then a specific time

Been writing a new function that will collect all VMs that are older than a specific time from a selected pool, you also have the option to delete them.

I got the idé from the following Reddit thread that I started where I do ask people what kind of scripts they want for vSphere and Horizon. I started the thread so I can create scripts for the community and challenge my self with new tasks.

I will keep writing scripts that people ask for during my free time and add it to my PowerShell module, vOperator.

So what this function will do is that it will collect all VMs that are older then what you choose from a specific Horizon pool, you have the option to only collect and show them but also the option to delete them.

You will need my vOperator PowerShell module to use this, if you don’t have it you can find out how to install it here.

With vOperator module this is a simple task to do, I’ll walk you through it.

First we need to fill some parameters out and connect to one of the connection servers to collect authentication information that we will need to talk with the rest API.

When you execute that you will be prompted to enter your credentials for Horizon, enter them without @domain.com.

# Fill this variables out
$hvFQDN = "YOURFQDN" # Example connectionserver.stolpe.io
$Domain = "YOURDOMAIN" # Example stolpe.io

# Connecting to Horizon and collecting authentication information that we need
$hvConnect = Connect-hvSrv -hvFQDN $hvFQDN -Cred (Get-Credential) -Domain $Domain

Now we need to collect all the VMs that we want, but we can use different parameters to filter the result.
We can use the -State parameter to decide what state the VMs that will be returned should be in. You can use the following options Available, Assigned, Unassigned, Archived
Default if you don’t use this parameter is Available.

To select the age limit you can use the parameters –Days , -Hours and -Minutes.
Default this parameters are set to 0 (zero)

We also have the option to just collect all of the VMs without deleting them, for that we can use the -Collect switch.

And sometime we want to run this script and confirm before it delete the VMs, then we can use the -Confirm switch.

Now it’s time to execute it, here are some examples.

# Example below will show how we are collecting all VMs from the pool "test" that are available according to there state and are more then 1 day old.
# This will not delete the VMs
$CollectVMs = $hvConnect | Get-hvVirtualMachineAge -PoolName "test" -State Available -Days 1 -Collect

# Here we are deleting the VMs from the pool "test" if the VM are more than 12 hours old.
$CollectVMs = $hvConnect | Get-hvVirtualMachineAge -PoolName "test" -State Available -Hours 12

# And now we do delete VMs that are 2 days 4 hours and 12 minutes old or older but we need to confirm it before they will get deleted.
$CollectVMs = $hvConnect | Get-hvVirtualMachineAge -PoolName "test" -State Available -Days 2 -Hours 4 -Minutes 12 -Confirm

Here you can see some pictures of the different results that you should see after running the different parts of the code above

For the end of the script we make the functions to display the output and also disconnect from Horizon.

if ($CollectVMs.ReturnCode -eq 0) {
    $CollectVMs.Message
}
else {
    $CollectVMs.Message
    $CollectVMs.ReturnValue
}

$hvDisconnect = $hvConnect | Disconnect-hvSrv
$hvDisconnect.Message

I do hope you did enjoy this script, I’ll continue to update the vOperator module with new functions and make it even better. And yes, I know I need to clean the code up, I’ll do that as soon as possible I promise!

You can find the script at Github , or just take a look below.

$hvFQDN = "YOURFQDN" # Example connectionserver.stolpe.io
$Domain = "YOURDOMAIN" # Example stolpe.io

$hvConnect = Connect-hvSrv -hvFQDN $hvFQDN -Cred (Get-Credential) -Domain $Domain

# Here we are deleting the VMs from the pool "test" if the VM are more than 12 hours old.
$CollectVMs = $hvConnect | Get-hvVirtualMachineAge -PoolName "test" -State Available -Hours 12

if ($CollectVMs.ReturnCode -eq 0) {
    $CollectVMs.Message
}
else {
    $CollectVMs.Message
    $CollectVMs.ReturnValue
}

$hvDisconnect = $hvConnect | Disconnect-hvSrv
$hvDisconnect.Message

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.