Hey, Scipting Guy! How Can I Notify a VMM Self-Service User That Their Virtual Machine Is About to Expire?

ScriptingGuy1

Bookmark and Share
 

About the author: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she’s broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, discovering new wines, and painting.

 

Welcome to the fourth and final installment (for now anyway) of scripting for System Center Virtual Machine Manager (VMM). The sun has finally decided to show itself, though it is also currently hailing. However, this weekend is supposed to be sunny and 60 degrees. My co-worker is calling this weekend “the heat wave of 2010.” He can’t wait to go out on his motorcycle and I can’t wait to soak up the rays. So, let’s bring together all that we’ve covered over the last few days.

This week, you have learned how to create virtual machines, move them between hosts and the library, and modify them. Today, we will use some core Windows PowerShell cmdlets in conjunction with VMM cmdlets to notify a self-service user that their virtual machine is about to expire. If, after notification, they do nothing, we disable the virtual machine by changing its owner (self-service users can only take actions on virtual machines that they own, or are owned by a group of which they are a member), and eventually move it to the library. Because this is a script that you might want to set up to run every day, we will cover how to run the script using Windows Task Scheduler.

To run today’s script, you can start with the virtual machines you created with the scripts in yesterday’s post, with expiration dates added to the custom 1 property. You might want to add a few more virtual machines with expiration dates that are 5 days out, today, yesterday, and 30 days ago to run the script against. The following script performs these actions:

1.       Calculates expiration dates relative to today’s date to which we will compare the expiration dates contained in the custom 1 property of the virtual machines.

2.       Extracts the alias of the owner of the virtual machine and uses that to create an e-mail address.

3.       Sends an e-mail message to the owner of a virtual machine that will expire in 5 days, and an e-mail message to the owner of a virtual machine that will expire in 1 day.

4.       If the virtual machine has an expiration date of yesterday, changes the owner of the virtual machine so that the self-service user will no longer have access to the virtual machine.

5.       If the virtual machine has been expired for 30 days, moves it to the library.

# Get the VMM server.

$VMMServer = Get-VMMServer -ComputerName “VMMServer01.Contoso.com”

 

# Calculate expiration dates at today plus 5 days, today, yesterday, and 30 days ago.

# We will compare these dates to the expiration date in the custom 1 property

# of the virtual machines.

$Expiry1 = (Get-Date).AddDays(+5)

$Expiry2 = Get-Date

$Expiry3 = (Get-Date).AddDays(-1)

$Expiry4 = (Get-Date).AddDays(-30)

 

# Get the host.

$VMHost = “VMHost01.Contoso.com”

 

# Get the virtual machines on the host where there is a value for the

# custom 1 property.

$VMs = Get-VM -VMHost $VMHost | where { $_.CustomProperties[0] -ne “”}

# Get the e-mail address of the Self-Service Administrator.

$SSAdmin = $VMMServer.SelfServiceContactEmail

# Loop through all of the virtual machines that have an expiration date.

ForEach ($VM in $VMs)

{

      # Get the owner of the virtual machine, and then remove the

      # preceding domain name. Use the resulting alias to construct

      # the owner’s e-mail address.

      $Owner = $VM.owner.Split(“”)

      $Alias = $Owner[1]

      $Email = $Alias + ” <$Alias@SMTPServer.Contoso.com>”

 

      # If the expiration date is 5 days from now, send the owner an e-mail.

      If ($VM.CustomProperties[0] -eq $Expiry1.ToShortDateString())

      {

            Write-Host $VM.Name “will expire in 5 days”

            Send-MailMessage -To $Email -From $SSAdmin -Subject “Your virtual machine will expire in 5 days” -Body “Your virtual machine is about to expire. Contact the Self-Service administrator to extend the expiration date.” -SmtpServer “SMTPServer.Contoso.com”

      }

 

      # If the expiration date is today, send the owner a reminder e-mail.

      ElseIf ($VM.CustomProperties[0] -eq $Expiry2.ToShortDateString())

      {

            Write-Host $VM.Name “expires today”

            Send-MailMessage -To $Email -From $SSAdmin -Subject “Your virtual machine expires today” -Body “Your virtual machine is about to expire. Contact the Self-Service administrator to extend the expiration date.” -SmtpServer “SMTPServer.Contoso.com”   

      }

 

      # If the expiration date was yesterday, change the owner so that the

      # original owner will no longer be able to access the virtual machine.

      Elseif ($VM.CustomProperties[0] -eq $Expiry3.ToShortDateString())

      {

            Write-Host $VM.name “has expired”

            Set-VM -VM $VM -Owner “ContosoPhyllis” | out-null

            Write-Host “Changed the owner of ” $VM.name

      }

     

      # If the expiration date was 30 days ago, move the virtual machine to the library.

      Elseif ($VM.CustomProperties[0] -eq $Expiry4.ToShortDateString())

      {

            Write-Host “Storing $VM in the library”

            If ($VM.StatusString -ne “Stopped”) {Stop-VM -VM $VM}

            Store-VM $VM –LibraryServer “LibServer.Contoso.com” –SharePath “\LibServer.Contoso.comlibrary” -RunAsynchronously

      }

}

 

To schedule this script to automatically run, create a task in the Windows Task Scheduler. The action in your task must start Powershell.exe and load the VMM snap-in in order to run the VMM cmdlets that are used by the script. Use the following syntax:

PowerShell.exe -PSConsoleFile “C:Program FilesMicrosoft System Center Virtual Machine Manager 2008 R2bincli.psc1” -Command “.’C:MyScriptsExpireSSVirtualMachines.ps1′”

 

It’s been a pleasure helping you discover the types of actions you can take on VMM objects using VMM cmdlets. I hope you have found these posts informative. Me, I’m off to find my sunscreen.

 

 

0 comments

Discussion is closed.

Feedback usabilla icon