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 has 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.

 

In the first two blog posts this week (Monday and Tuesday), we've been performing actions on existing Virtual Machine Manager (VMM) objects. Today, we're going to create virtual machines. There are a few ways in which you can do this, such as creating a virtual machine from a template, cloning an existing virtual machine, or converting  a physical machine (also known as P2V) or a VMware virtual machine (also known as V2V).

Today, we will focus on two of these: creating a virtual machine from a template, and cloning an existing virtual machine. If you're interested in the other methods of using scripts to create virtual machines, see the VMM 2008 Scripting Guide. After we create our virtual machines, we will then modify them by adding an expiration date to the custom 1 property.

We'll create our first virtual machine from an existing template. If you're not familiar with templates or how to create them, check out the topic, Creating Virtual Machines from a Template, on TechNet. To keep with the self-service theme we began yesterday, we'll set the owner to a self-service user account:

# Connect to the VMM server.

Get-VMMServer -ComputerName "VMMServe01.Contoso.com"

# Specify the template to use and the name of the virtual machine.

$VMTemplate = "VMMTemplate1"

$VMName = "VM01"

# Specify the host on which you want to place the virtual machine.

$VMHost = "VMMHost01.Contoso.com"

# Specify the path on the host.

$VMPath = "C:\VMs"

# Create the virtual machine.

New-VM -Name $VMName -Path $VMPath -Template $VMTemplate -VMHost $VMHost -Description "Self-Service Virtual Machine" -Owner "Contoso\SelfServiceUser" -ComputerName "*" -OrgName "Contoso"

If you need to create several virtual machines, you can modify the above script do to just that. See the CreateNewVMsFromTemplate.ps1 script on TechNet for an example.

Now we'll clone the virtual machine we just created:

# Connect to the VMM server. This is only necessary if you've opened a new command window.

Get-VMMServer -ComputerName "VMMServe01.Contoso.com"

# Get the virtual machine you created in the previous script.

$VM = Get-VM -Name "VM01"

# Check to see if the virtual machine is stopped. If not, stop the virtual machine.

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

# Get the host on which you want to deploy the virtual machine.

$VMHost = Get-VMHost -ComputerName "VMHost01.Contoso.com"

# Use the New-VM cmdlet to clone the virtual machine.

New-VM -Name "VM02" -VM $VM -VMHost $VMHost -Path "C:\VMs" -RunAsynchronously


Voilà: two new virtual machines. The next thing I want to do is add an expiration date to the custom 1 property of these virtual machines. We'll use the value of this property tomorrow to determine whether the virtual machine is close to its expiration date:

# Connect to the VMM server. This is only necessary if you've opened a new command window.

Get-VMMServer -ComputerName "VMMServer01.Contoso.com"

# Get the host that the virtual machines are on.

$VMHost = Get-VMHost -ComputerName "VMHost01.Contoso.com"

# Get the virtual machines.

$VMs = Get-VM -VMHost $VMHost

# Determine the expiration date. I'm using 90 days.

$ExpirationDate = (Get-Date).AddDays(90)

# Add the expiration date to the custom 1 property.

ForEach ($VM in $VMs) {Set-VM -VM $VM -Custom1 $ExpirationDate.ToShortDateString()}


Tomorrow we'll find the virtual machines that are near or at their expiration date, notify the owner that the virtual machine is about to expire, and then take action on the expired virtual machines.