Creating and Modifying a Script from a VMM Wizard

Letzte Aktualisierung: Oktober 2008

Betrifft: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

In System Center Virtual Machine Manager (VMM), you can create and then modify a script that is generated when you run a wizard in the VMM Administrator Console. The example in this topic shows you how to modify a script that is generated by the New Virtual Machine Wizard.

Using View Script to Create a Reusable Script

Every wizard in VMM contains a View Script button on the Summary page of the wizard. When you click the View Script button, Notepad opens a script that performs the same actions. You can use these scripts as a starting point for creating custom scripts to automate tasks in your VMM environment.

Viewing and Saving Script Output That Is Generated by the New Virtual Machine Wizard

The script in this example is a script that is generated by the New Virtual Machine Wizard. In this example, the wizard uses an existing template as the source object from which to create the virtual machine. The contents of the script vary depending on the options you choose when you run the wizard.

Hinweis

This example assumes that the Template1 virtual machine template already exists. For information about how to create a new template for VMM, type Get-Help New-Template -detailed at the Windows PowerShell - Virtual Machine Manager command prompt.

# ------------------------------------------------------------------------------
# New Virtual Machine Script
# ------------------------------------------------------------------------------
# Script generated on Thursday, August 21, 2008 11:26:50 PM by Virtual Machine Manager
# For additional help on cmdlet usage, type get-help <cmdlet name>
# ------------------------------------------------------------------------------

Set-VirtualFloppyDrive -RunAsynchronously -VMMServer localhost -NoMedia -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

Set-VirtualCOMPort -NoAttach -VMMServer localhost -GuestPort 1 -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

Set-VirtualCOMPort -NoAttach -VMMServer localhost -GuestPort 2 -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

New-VirtualNetworkAdapter -VMMServer localhost -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -PhysicalAddressType Dynamic -VLanEnabled $false 

New-VirtualDVDDrive -VMMServer localhost -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -Bus 1 -LUN 0 

$CPUType = Get-CPUType -VMMServer localhost | where {$_.Name -eq "1.20 GHz Athlon MP"}

New-HardwareProfile -VMMServer localhost -Owner "CONTOSO\Phyllis" -CPUType $CPUType -Name "Profile2d4c4e4d-b773-431c-922f-242f4c83f28c" -Description "Profile used to create a VM/Template" -CPUCount 1 -MemoryMB 512 -ExpectedCPUUtilization 20 -DiskIO 0 -CPUMax 100 -CPUReserve 0 -NetworkUtilization 0 -RelativeWeight 100 -HighlyAvailable $false -NumLock $false -BootOrder "CD", "IdeHardDrive", "PxeBoot", "Floppy" -LimitCPUFunctionality $false -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

$Template = Get-Template -VMMServer localhost | where {$_.Name -eq "Template1"}
$VMHost = Get-VMHost -VMMServer localhost | where {$_.Name -eq "VMHost1.Contoso.com"}
$HardwareProfile = Get-HardwareProfile -VMMServer localhost | where {$_.Name -eq "Profile2d4c4e4d-b773-431c-922f-242f4c83f28c"}

$OperatingSystem = Get-OperatingSystem -VMMServer localhost | where {$_.Name -eq "64-bit edition of Windows Server 2008 Standard"}

New-VM -Template $Template -Name "SampleVM" -Description "" -VMHost $VMHost -Path "C:\VirtualMachines" -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -RunAsynchronously -Owner "CONTOSO\Phyllis" -HardwareProfile $HardwareProfile -ComputerName "*" -FullName "" -OrgName "" -ProductKey "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" -TimeZone 4 -JoinWorkgroup "WORKGROUP"  -AnswerFile $null -OperatingSystem $OperatingSystem -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM 

Save the file as NewVMScriptFromWizard.ps1.

Modifying Input Options

Decide which options you want to provide as input to the script. The following example allows users to modify the virtual machine name, the amount of memory, and the name of the destination host. Additionally, it sets the computer name of the virtual machine to be the same as the virtual machine name.

Add the following lines to the script after the comment lines at the top of the script.

# --------------------------------------------------------------------
# Define the variables.
# --------------------------------------------------------------------
$VMName = $args[0]
$Memory = $args[1]
$VMHostName = $args[2]
$ComputerName = $VMName

These assignment statements tell the script to store the first argument that is passed to the script to the $VMName variable, the second argument to the $Memory variable, and the third argument to the $VMHostName variable. Finally, the $ComputerName variable is set to the same value as $VMName. Alternatively, you can set $ComputerName to $args[4].

Update the Script to Use the New Variables

Next, update the commands in the original script to use the parameters and the variables that you defined. The following code segments make each of these changes.

Update the New-HardwareProfile command

Update the New-HardwareProfile command to use the new $Memory variable, as follows.

New-HardwareProfile -VMMServer localhost -Owner "CONTOSO\Alex" -CPUType $CPUType -Name "Profilec331c4d2-bccb-46f7-8ff4-000abe429cb3" -Description "Profile used to create a VM/Template" -ProcessorCount 1 -MemoryMB $Memory -ExpectedCPUUtilization 20 -DiskIO 0 -NetworkUtilization 10 -RelativeWeight 100 -JobGroup e245e50f-6955-4837-8639-3e027c47b89f 

Update the Get-VMHost command

Update the Get-VMHost command to use the new $VMHostName variable:

$VMHost = Get-VMHost -VMMServer localhost | where {$_.Name -eq $VMHostName}

Update the New-VM command

Update the New-VM command to use the new $VMName and $ComputerName variables:

New-VM -Template $Template -Name $VMName -Description "" -VMHost $VMHost -Path "C:\Documents and Settings\All Users\Documents\Shared Virtual Machines\" -JobGroup 12eb89de-4b8d-4a14-9758-0b02215645a5 -RunAsynchronously -Owner "CONTOSO\Alex" -HardwareProfile $HardwareProfile -ComputerName $ComputerName -FullName "User" -OrgName "Company" -ProductKey "12345-12345-12345-12345-12345" -TimeZone 4 -JoinWorkgroup "WORKGROUP"  -RunAsSystem -UseHardwareAssistedVirtualization $false -StopAction SaveVM

Viewing the Updated Script

Your script should resemble the following script.

Hinweis

This sample script assumes that the SampleTemplate virtual machine template already exists.

# ------------------------------------------------------------------------------
# New Virtual Machine Script
# ------------------------------------------------------------------------------
# Script generated on Thursday, August 21, 2008 11:26:50 PM by Virtual Machine Manager
# For additional help on cmdlet usage, type get-help <cmdlet name>
# ------------------------------------------------------------------------------

# --------------------------------------------------------------------
# Define script parameters and variables.
# --------------------------------------------------------------------
$VMName = $args[0]
$Memory = $args[1]
$VMHostName = $args[2]
$ComputerName = $VMName
Set-VirtualFloppyDrive -RunAsynchronously -VMMServer localhost -NoMedia -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

Set-VirtualCOMPort -NoAttach -VMMServer localhost -GuestPort 1 -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

Set-VirtualCOMPort -NoAttach -VMMServer localhost -GuestPort 2 -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

New-VirtualNetworkAdapter -VMMServer localhost -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -PhysicalAddressType Dynamic -VLanEnabled $false 

New-VirtualDVDDrive -VMMServer localhost -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -Bus 1 -LUN 0 

$CPUType = Get-CPUType -VMMServer localhost | where {$_.Name -eq "1.20 GHz Athlon MP"}

New-HardwareProfile -VMMServer localhost -Owner "CONTOSO\Phyllis" -CPUType $CPUType -Name "Profile2d4c4e4d-b773-431c-922f-242f4c83f28c" -Description "Profile used to create a VM/Template" -CPUCount 1 -MemoryMB $Memory -ExpectedCPUUtilization 20 -DiskIO 0 -CPUMax 100 -CPUReserve 0 -NetworkUtilization 0 -RelativeWeight 100 -HighlyAvailable $false -NumLock $false -BootOrder "CD", "IdeHardDrive", "PxeBoot", "Floppy" -LimitCPUFunctionality $false -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 

$Template = Get-Template -VMMServer localhost | where {$_.Name -eq "Template1"}
$VMHost = Get-VMHost -VMMServer localhost | where {$_.Name -eq "$VMHostName"}
$HardwareProfile = Get-HardwareProfile -VMMServer localhost | where {$_.Name -eq "Profile2d4c4e4d-b773-431c-922f-242f4c83f28c"}

$OperatingSystem = Get-OperatingSystem -VMMServer localhost | where {$_.Name -eq "64-bit edition of Windows Server 2008 Standard"}

New-VM -Template $Template -Name "$VMName" -Description "" -VMHost $VMHost -Path "C:\VirtualMachines" -JobGroup dc5e4f82-c63f-4f51-922e-86a6d69ef1d9 -RunAsynchronously -Owner "CONTOSO\Phyllis" -HardwareProfile $HardwareProfile -ComputerName "$ComputerName" -FullName "" -OrgName "" -ProductKey "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" -TimeZone 4 -JoinWorkgroup "WORKGROUP"  -AnswerFile $null -OperatingSystem $OperatingSystem -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM

Using NewVMScriptFromWizard.ps1

You can run the NewVMScriptFromWizard.ps1 script in the VMM command shell to create new virtual machines, as shown in the following examples.

The following command uses the script to create a virtual machine named SampleVM01 that has 256 megabytes (MB) of memory. The command places the virtual machine on host VMHost01:

NewVMScriptFromWizard.ps1 "SampleVM01" 256 "VMHost01"

The following command uses the script to create a virtual machine named SampleVM02 that has 512 MB of memory. The command places the virtual machine on host VMHost02:

NewVMScriptFromWizard.ps1 "SampleVM02" 512 "VMHost02"