Partager via


RapidProvisionVM.ps1

Mis à jour: août 2009

S'applique à: Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

To quickly create virtual machines from a template, you can use the UseLocalVirtualHardDisks parameter of the New-VM cmdlet. This parameter specifies that New-VM use an existing virtual hard disk file stored locally on the destination host instead of copying a virtual hard disk file from the library by using Background Intelligent Transfer Service (BITS). Use the Move-VirtualHardDisk cmdlet to specify the local virtual hard disk file that the virtual machine should connect to in place of the virtual hard disk file that the template points to.

Before you use the script in this topic, ensure that the following actions have been completed:

  1. You have created a template from a virtual machine that contains the operating system and any desired applications, and you have configured the template to meet all user requirements. Optionally, the virtual machine from which the template is created can have Integration Components installed. Confirm that the template:

    • Uses the virtual hard disk file that is attached to the source virtual machine from which the template was created.

    • Contains updated Hardware Configuration settings.

    • Contains updated OS Configuration settings, such as a product ID.

  2. You have copied the generalized virtual hard disk file to the destination host. This location needs to be specified in the script. You can copy the virtual hard disk file directly to the host using a command such as robocopy or xcopy. Or, within a storage area network (SAN) environment, you can use a SAN technology, such as one of the following:

    • Snapshot of an existing logical unit number (LUN)

    • Clone of an existing LUN

You can also use this script to create a virtual machine by using a template with an embedded answer file. For more information, see RapidProvisionVMwithAnswerFile.ps1.

Disclaimer

Before you run the following script, replace the values for the name of the VMM server, the template name, the name of the destination host for the virtual machine, the path of the local virtual hard disk file, and the path where you want to create the virtual machine.

# Filename:        RapidProvisionVM
# Description:     Creates a new virtual machine using a virtual hard disk 
#                  that has been generalized by Virtual Machine Manager (VMM) and 
#                  that is stored locally on a Hyper-V host.

# Connect to the VMM server.
$VMMServer = Get-VMMServer "VMMServer01.Contoso.Com"

# Define the variables.
$JobGroupID = [Guid]::NewGuid().ToString()
$Template = Get-Template | where {$_.Name -eq "Template01"}
$VMHost = Get-VMHost | where {$_.ComputerName -eq "VMMHost01.Contoso.Com"}
$VMName = "VM01"

# Use the Move-VirtualHardDisk cmdlet to point to the local virtual hard disk on the host.
Move-VirtualHardDisk -IDE -BUS 0 -LUN 0 -Path "c:\VHDs\LocalVHDFile.vhd" -JobGroup $JobGroupID

# Use the New-VM cmdlet with the –UseLocalVirtualHardDisks parameter to create the 
# virtual machine and to specify that the local virtual hard disk file should be used.
New-VM -Name $VMName -Path "D:\VirtualMachinePath" -Template $Template -VMHost $VMHost -ComputerName $VMName -JobGroup $JobGroupID -UseLocalVirtualHardDisks -RunAsynchronously -JobVariable "NewVMJob"

# Display the job progress.
While ($NewVMJob.status -eq "Running")
{
   Write-Progress -activity "Creating new virtual machine" -status $NewVMJob.CurrentStep
   Start-Sleep -seconds 5
}