Using Annotations to Terminate VMs based on Date (Part 1)

When users request a number of VMs which are not going to persistent in your datacenter this can lead to a large VM sprawl if not managed correctly. One method to do this would be to create a number of virtual machine type annotations based on the creation of the VM to include tags to  filter VMs that are now longer required.

In this example, I will be using the following virtual machine type annotations to filter VMs:

  • Termination Date
  • Requested By
  • Service Request

When creating VMs the above annotations will be require the value to be populated in the following format:

Annotation Type Value
Termination Date Date dd/MM/yyyy
Requested By Email user@domain.com
Service Request URL 1234

The requirements will be to alert via email those users who have requested VMs that are to expire in the next three days and provide the URL to the original service request if they require to extend the lifetime.

This process can run on a daily schedule by invoking the powershell script where the VMware PowerCLI cmdlets are loaded into the session.

So lets get started and load the cmdlets and connect to the a vcenter server (in this example, server1.domain.local).

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) 
   { 
   Add-PSSnapin VMware.VimAutomation.Core > $null
   } 

Connect-VIServer server1.domain.local

Once we have the cmdlets loaded in our powershell session and connected to the vcenter server, we will build a collection of VMs using the Get-VM cmdlet and loop through each VM returned in the collection:

$VMs = Get-VM 

ForEach ($VM in $VMs)
{

We will now return the value of the virtual machine type annotations listed above;

$Termination = (Get-VM $VM | Get-Annotation | Where-Object {$_.Name -eq "Termination Date"}).Value 
$RequestedBy = (Get-VM $VM | Get-Annotation | Where-Object {$_.Name -eq "Requested By"}).Value
$ServiceRequest = (Get-VM $VM | Get-Annotation | Where-Object {$_.Name -eq "Service Request"}).Value

In order to compate the termination date we will use conditional logic to determine if the termination date is greater than three days in the past

If ($Termination -gt (((Get-Date).AddDays(-3)).ToString("dd/MM/yyyy")))

If the statement is true will generate the URL of the service request by appending the $ServiceRequest variable to the link structure and send an email to the user who requested the VM to include the termination date and the link to extend the lifetime of the VM.

{
$URL = ("http://Server/Departments/Helpdesk/Lists/Service%20Requests/DispForm.aspx?ID=$ServiceRequest")
Send-MailMessage -From admin@domain.com -To $RequestedBy -Subject "VM Termination Notice" -Body "The virtual machine $VM you requested is due for termination on $Termination.`n`nIn order to extend the lifetime of this virtual machine, please update $URL." -SmtpServer server2.domain.local
} 

}

The user whom made the request will now receive an email to notify that the VM is nearing the end of its lifetime and is due for termination, as below:

TerminationCapture

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s