VMM: Script VM Additions in PS

The following information on scripting the installation of VM Additions using VMM/PowerShell is copied from the VMM Scripting Guide, coming to the SCVMM Library on TechNet soon.

InstallVMAdditions.ps1

You can use the InstallVMAdditions.ps1 script to install Virtual Machine Additions on virtual machines that are managed by Virtual Machine Manager.

For any virtual machine to function well, you must install Virtual Machine Additions on that virtual machine. Virtual Machine Additions, which is provided by Virtual Server 2005, improves the performance of the guest operating system, enables the free movement of the mouse between the virtual machine window and the host operating system, optimizes video drivers, and supports time synchronization.

For a related script that identifies which virtual machines in your Virtual Machine Manager environment do not yet have Virtual Machine Additions installed, see NeedVMAdditions.ps1 in the topic, "Sample Job-Related Scripts" in the VMM Scripting guide.

Define Variables for InstallVMAdditions.ps1

The first commands in InstallVMAdditions.ps1 define variables that the script uses to install Virtual Machine Additions. As indicated in the comment introducing $LinkIso, you have the option to set $LinkIso to $TRUE or $FALSE depending on whether you want to link to the VMAdditions.iso or copy it onto the virtual machine.

####################################################################

# Define variables.

####################################################################

$VMMServer = "localhost"

$VMNameToInstallVMAdditions = "VM01"

# If you want to link to VMAdditons.iso in the Virtual Machine Manager

# library, set $LinkIso to $TRUE; alternatively, if you want to copy

# VMAdditions.iso to the VM, set $LinkIso to $FALSE.

$LinkIso = $FALSE

Connect to the Virtual Machine Manager Server

The next command in InstallVMAdditions.ps1connects to the Virtual Machine Manager server so that subsequent commands can access objects in the Virtual Machine Manager database.

####################################################################

# Connect to the Virtual Machine Manager server.

####################################################################

$C = Get-VMMServer $VMMServer

Get the Virtual Machine on Which to Install Virtual Machine Additions.

The next command in InstallVMAdditions.ps1 retrieves the object that represents the virtual machine whose name was stored earlier in variable $VMNameToInstallVMAdditions.

####################################################################

# Get the VM on which you want to install VM Additions.

####################################################################

$VM = Get-VM | where {$_.Name -eq $VMNameToInstallVMAdditions}

Install Virtual Machine Additions or Display an Error Message

The next set of commands in InstallVMAdditions.ps1 performs a series of nested tests to determine whether it is possible to install Virtual Machine Additions and, if any test fails, displays an error message indicating what you need to do to solve the problem.

· Is the virtual machine installed on a host?

· If yes, continue to the next question.

· If not, the script ends and displays a message indicating that it cannot install Virtual Machine Additions because the virtual machine is currently stored in the library. You must deploy the virtual machine on a host and then run the script again. For more information, type the following command at the command prompt:

Get Help Move-VM -detailed

· Does the virtual machine have at least one virtual DVD drive?

· If yes, the script does the following:

Stores host object. Stores the object that represents the host on which this virtual machine is deployed in variable $VmHost.

Stores ISO name string. Uses the command segment $VmHost.VirtualizationStack to determine which version of Virtual Server is running on the host (because Virtual Machine Additions varies depending on the Virtual Server version); concatenates the Virtual Server version with the string " VMAdditions"; and then stores the resulting name in variable $Isoname. The example assumes that Virtual Server version plus " VMAdditions" is the naming convention that is used to identify different versions of Virtual Machine Additions.

Gets ISO object. Gets the object that represents the Virtual Machine Additions .iso file in variable $VmAdditions.Iso.

Continue to the next question.

· If not, the script ends and displays a message indicating that it cannot install Virtual Machine Additions because no virtual DVD drive exists on the virtual machine. You must configure a virtual DVD drive for this virtual machine and then run the script again. For more information, type the following command at the command prompt:

Get-Help New-VirtualDVDDrive -detailed

· Does at least one Virtual Machine Additions .iso file (of the correct version) exist in the library?

· If yes, continue to the next question.

· If not, the script ends and displays a message indicating that it cannot install Virtual Machine Additions because no Virtual Machine Additions .iso file exists in the Virtual Machine Manager library. You must add an .iso file for Virtual Machine Additions to a Virtual Machine Manager library share and then run the script again.

After you add the .iso file to a library share, the Virtual Machine Manager refresher automatically scans the share, discovers the new .iso file, and adds an object that represents the .iso file to the library catalog. Alternatively, you can use the Refresh-LibraryShare cmdlet to refresh the library share immediately.

For more information, type the following command at the command prompt:

Get-Help Refresh-LibraryShare -detailed

Note You can copy the VMAdditions.iso file from Microsoft Virtual Server 2005, which is installed by default with Virtual Machine Manager. You can find the VMAdditions.iso file in Windows Explorer at <C>:\Program Files\Microsoft Virtual Server\Virtual Machine Additions.

· Do you want to link to or copy the .iso file?

· Link to? If you have set $LinkIso to $TRUE, the script uses the pipeline operator (|) to pass the first virtual DVD drive (indicated by [0]) on the virtual machine to the Set-VirtualDVDDrive cmdlet, which links to the Virtual Machine Additions .iso file in the library.

The command segment -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun must be specified when changing the attached .iso file of a virtual DVD drive. In this case, the script does not modify where the virtual DVD drive is attached, so it simply sets the -Bus and -Lun parameters to the current Bus and Lun settings. Finally, the script stores the resulting object in variable $DVDDrive in case you want to use this variable later on.

· Copy? If you have set $LinkIso to $FALSE, the script uses the pipeline operator (|) to pass the first virtual DVD drive (indicated by [0]) on the virtual machine to the Set-VirtualDVDDrive cmdlet, which copies the Virtual Machine Additions .iso file onto the virtual machine.

The command segment -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun must be specified when changing the attached .iso file of a virtual DVD drive. In this case, the script does not modify where the virtual DVD drive is attached, so it simply sets the -Bus and -Lun parameters to the current Bus and Lun settings. Finally, the script stores the resulting object in variable $DVDDrive in case you want to use this variable later on.

Note The second command omits the -Link parameter but uses the $FALSE value specified earlier. By default, if -Link is omitted, the Set-VirtualDVDDrive cmdlet copies a file rather than linking to it.

· Link to or Copy—same result: After the script either runs the command to link to the Virtual Machine Additions .iso file in the library or runs the command to copy the .iso file to the virtual machine, the script displays a message indicating that the Virtual Machine Additions .iso file is now attached to the virtual machine and requests the user to start the virtual machine and install Virtual Machine Additions from the virtual DVD drive.

####################################################################

# Install VM Additions or (if not possible) display an error message.

####################################################################

if($VM.HostType -eq "VMHost")

{

    if($VM.VirtualDVDDrives.Count -gt 0)

    {

      $VmHost = $VM.VMHost

      # The segment$VmHost.VirtualizationStack returns the version

      # of Virtual Server running on this host.

      $IsoName = $VmHost.VirtualizationStack +" VMAdditions"

      $VmAdditionIso = Get-ISO | where {$_.Name -eq $IsoName}

      if($Null -ne $VmAdditionIso)

        {

          if($LinkIso -eq $TRUE)

       {

              $DvdDrive = $VM.VirtualDVDDrives[0] | set-VirtualDVDDrive -ISO $VmAdditionIso -Link -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun

          }

          else

          {

              $DvdDrive = $VM.VirtualDVDDrives[0] | set-VirtualDVDDrive -ISO $VmAdditionIso -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun

          }

          echo "Attached " $VmAdditionIso.Name " to VM" $VM.Name " Please start the vm if not running and install additions from dvddrive"

      }

      else

      {

           echo "Can't install vmadditions on VM " $VM.Name " as $IsoName ISO not present, please add it to library and retry"

      }

  }

  else

  {

      echo "Can't install vmadditions on VM " $VM.Name " has no VirtualDVDDrives present, please add a dvddrive and retry"

      

  }

}

else

{

    echo "Can't install vmadditions on VM " $VM.Name " in library, please deploy to host and retry"

}

InstallVMAdditions.ps1 - Complete Script

Copy the following complete version of InstallVMAdditions.ps1 into a Notepad file, and save it as InstallVMAdditions.ps1.

# Filename: InstallVMAdditions.ps1

# Description: Create a System Center Virtual Machine Manager-based

# hardware profile and operating-system profile; use

# these profiles and a sysprepped VHD to create a

# template; use the template to create 'n' number of

# virtual machines, adding an additional VHD to each

# new VM; and deploy each virtual machine on the most

# suitable host. Display progress messages for each task

# and display a final status message for attempt to

# create a virtual machine.

 

# DISCLAIMER:

# Copyright © Microsoft Corporation. All rights reserved. This

# script is made available to you without any express, implied or

# statutory warranty, not even the implied warranty of

# merchantability or fitness for a particular purpose, or the

# warranty of title or non-infringement. The entire risk of the

# use or the results from the use of this script remains with you.

 

####################################################################

# Define variables

####################################################################

$VMMServer = "localhost"

$VMNameToInstallVMAdditions = "VM01"

# If you want to link to VMAdditons.iso in the Virtual Machine Manager

# library, set $LinkIso to $TRUE; alternatively, if you want to copy

# VMAdditions.iso to the VM, set $LinkIso to $FALSE.

$LinkIso = $FALSE

 

####################################################################

# Connect to the Virtual Machine Manager server.

####################################################################

$C = Get-VMMServer $VMMServer

 

####################################################################

# Get the VM on which you want to install VM Additions.

####################################################################

$VM = Get-VM | where {$_.Name -eq $VMNameToInstallVMAdditions}

 

####################################################################

# Install VM Additions or (if not possible) display an error message.

####################################################################

 

if($VM.HostType -eq "VMHost")

{

    if($VM.VirtualDVDDrives.Count -gt 0)

    {

      $VmHost = $VM.VMHost

      # The segment$VmHost.VirtualizationStack returns the version

      # of Virtual Server running on this host

      $IsoName = $VmHost.VirtualizationStack +" VMAdditions"

      $VmAdditionIso = Get-ISO | where {$_.Name -eq $IsoName}

      if($Null -ne $VmAdditionIso)

        {

          if($LinkIso -eq $TRUE)

          {

              $DvdDrive = $VM.VirtualDVDDrives[0] | set-VirtualDVDDrive -ISO $VmAdditionIso -Link -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun

          }

          else

          {

              $DvdDrive = $VM.VirtualDVDDrives[0] | set-VirtualDVDDrive -ISO $VmAdditionIso -Bus $VM.VirtualDVDDrives[0].Bus -Lun $VM.VirtualDVDDrives[0].Lun

          }

          echo "Attached " $VmAdditionIso.Name " to VM" $VM.Name " Please start the vm if not running and install additions from dvddrive"

      }

      else

      {

           echo "Can't install vmadditions on VM " $VM.Name " as $IsoName ISO not present, please add it to library and retry"

   }

  }

  else

  {

      echo "Can't install vmadditions on VM " $VM.Name " has no VirtualDVDDrives present, please add a dvddrive and retry"

      

  }

}

else

{

    echo "Can't install vmadditions on VM " $VM.Name " in library, please deploy to host and retry"

}