RapidProvisionVMwithAnswerFile.ps1

Applies To: Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

The RapidProvisionVM.ps1 topic introduced a script that you can use to create a virtual machine that connects to an existing virtual hard disk (.vhd) file stored locally on a destination host. You can also use this script to create a virtual machine from a template that contains an embedded answer file and Integration Components. Because Integration Components are already installed, use the SkipInstallVirtualizationGuestServices parameter of the New-VM cmdlet in the script to specify that New-VM should not attempt to install Integration Components after creating the virtual machine.

Before you use the script, follow these steps:

  1. Create a virtual machine that contains an operating system, and configure all the settings to meet user requirements. Optionally, you can install Integration Components; however, this may not be necessary, depending upon your operating system.

  2. Create an answer file, and store it in the file system of the virtual machine that contains the guest operating system. The answer file should include the following:

    • Product key

    • Credentials with permissions to join a domain

    • The computer name wildcard character (*), which allows the computer name to be randomly generated

  3. Generalize the guest operating system using the Sysprep tool, and then shut down the virtual machine.

  4. Copy the generalized .vhd 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:

    • A snapshot of an existing logical unit number (LUN)

    • A clone of an existing LUN

  5. Create a new template from a .vhd file (this can be any .vhd file because it is a placeholder). When you create the new template:

    • On the Configure Hardware page of the New Template Wizard, update the settings as necessary.

    • On the Guest Operating System page of the wizard, select Customization not required in the Guest operating system profile list.

Before you run the following script to create a virtual machine, replace the values for the VMM server name, 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.

Disclaimer

# Filename:        RapidProvisionVMsWithAnswerFile.ps1
# Description:     Creates a new virtual machine using a virtual hard disk (.vhd)
#                  that has an embedded answer file that has been generalized by Virtual 
#                  Machine Manager (VMM), and that is stored locally on a Hyper-V host.

# Connect to the VMM server.
$VMServer = 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 .vhd file 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 .vhd file should be used.
# Additionally, use the -SkipInstallVirtualizationGuestServices parameter because 
# Integration Components are already installed in the .vhd file.

New-VM -Name $VMName -Path "D:\VirtualMachinePath" -Template $Template -VMHost $VMHost -JobGroup $JobGroupID -UseLocalVirtualHardDisks -SkipInstallVirtualizationGuestServices -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
}

See Also

Concepts

RapidProvisionVM.ps1