CreateVMInLibraryMoveToHost.ps1
Applies To: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1
You can create virtual machines in the library from a blank virtual hard disk, from an existing virtual hard disk, or from an existing virtual machine. To use the virtual machine, deploy it to a host by using the Move-VM cmdlet. To store a virtual machine in the library, you must stop the virtual machine, shut down the virtual machine, or place the virtual machine in a saved state. Then, you can use the Store-VM cmdlet to store the virtual machine.
The following script creates a virtual machine in the library from a blank virtual hard disk. Then, the script deploys the virtual machine on the best-rated host in the host group. Finally, the script stops the virtual machine and stores it in the library.
# Filename: CreateVMinLibraryMovetoHost.ps1
# Description: Creates a virtual machine from a blank
# virtual hard disk and stores the virtual
# machine in the library. Then, the script deploys
# the virtual machine to an appropriate host based
# on host ratings. Finally, the script stops the
# virtual machine and stores it in the library.
# Connect to the Virtual Machine Manager (VMM) server.
$VMMServer = Get-VMMServer -ComputerName "VMMServer1.contoso.com"
# Get the library server and provide the library share path.
$VMMLibrary = Get-LibraryServer -VMMServer $VMMServer -ComputerName "VMMLibrary.Contoso.com"
$VMMLibraryPath = "\\VMMLibrary.Contoso.com\LibraryShare\VMs"
# Supply a name for the virtual machine and a virtual hard disk
# from which to create the virtual machine.
$VMName = "VM01"
$VHD = Get-VirtualHardDisk | where { $_.Name -eq "Blank Disk - Small" }
# Supply the host group in which to deploy the virtual machine later in the script.
$VMHostGroup = Get-VMHostGroup -Name "All Hosts"
# Create a virtual machine and place it in the library.
New-VM -Name $VMName -LibraryServer $VMMLibrary -SharePath $VMMLibraryPath -VirtualHardDisk $VHD -Description "Virtual machine created in the library" -Owner "CONTOSO\Phyllis" -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM
# Pause the script for 30 seconds to verify that the virtual machine exists in the library.
Start-Sleep -s 30
# Deploy the virtual machine to an appropriate host based on host ratings.
# Get and sort the host ratings for all the hosts in the host group.
$HostRatings = @(Get-VMHostRating -VM $VMName -VMHostGroup $VMHostGroup | where { $_.Rating -gt 0 } | Sort-Object -property Rating -descending)
If($HostRatings.Count -eq "0") { throw "No hosts meet the requirements." }
# If there is at least one host that will support the virtual machine,
# move the virtual machine to the highest-rated host.
If ($HostRatings.Count -ne 0)
{
$VMHost = $HostRatings[0].VMHost
$VMPath = $HostRatings[0].VMHost.VMPaths[0]
# Move the virtual machine.
Write-Host "Moving $VMName to $VMHost"
Move-VM -VM $VMName -VMHost $VMHost -Path $VMPath -StartVMOnTarget
# Pause the script for 30 seconds to verify that the virtual machine was deployed on the host.
Start-Sleep -s 30
# Stop the virtual machine and then store it in the library.
Stop-VM -VM $VMName
Write-Host "Moving $VMName to $VMMLibraryPath"
Store-VM -VM $VMName -LibraryServer $VMMLibrary -SharePath $VMMLibraryPath -RunAsynchronously
}