Sample Script for Upgrading Hosts and Library Servers

You can experiment with the Windows PowerShell script that is described in this topic to learn how to automate updating the agent software on hosts and library servers that are managed by Virtual Machine Manager.

UpgradeAgents.ps1

After you upgrade your Virtual Machine Manager server to a later version of the Virtual Machine Manager service, update the Virtual Machine Manager agent software that is installed on computers that are managed by that Virtual Machine Manager server to the current version of the agent software. You can use the UpgradeAgents.ps1 script to automate that update.

Managed computers on which you might need to update the agent software include:

  • Host servers on which you deploy virtual machines.
  • Library servers on which you store library resources.

How UpgradeAgents.ps1 Works

The UpgradeAgents.ps1 script performs the following tasks:

  • Connects to the Virtual Machine Manager server and retrieves the object that represents the server from the Virtual Machine Manager database. Connecting to the Virtual Machine Manager server retrieves the server object from the Virtual Machine Manager database and gives you access to all other objects in the SQL Server database that stores all Virtual Machine Manager objects for as long as the connection lasts or until you close the command-shell window.
  • Uses the Virtual Machine Manager cmdlet, Get-VMMManagedComputer, to get the objects that represent all computers managed by Virtual Machine Manager (that is, to retrieve from the Virtual Machine Manager database the objects for all virtual machine hosts and all library servers); uses the standard Windows PowerShell cmdlet, Where-Object, (whose alias is "where") to select from the set of all managed computers only objects for those managed computers whose agent software is not current; and stores the selected computer objects in variable $VMMManagedComputers.
  • Uses the standard Windows PowerShell cmdlet, Get-Credential, to prompt you to provide credentials that have permissions to update computers that are managed by Virtual Machine Manager.
  • Uses a ForLoop to perform the following tasks:
    • Loops through each object in $VMMManagedComputer in turn.
    • Uses the Virtual Machine Manager cmdlet, Update-VMMManagedComputer, to install updated agent software on each managed computer, by using the credentials you provided earlier for each update.

UpgradeAgents.ps1 - Complete Script

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

# Filename:    UpgradeAgents.ps1.ps1
# Description: Updates the System Center Virtual Machine Manager
#              agent software on any managed computer (that is, any 
#              host or library server) that is running an earlier 
#              version of the agent software.

# DISCLAIMER:
# Copyright (c) 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.

####################################################################
# Connect to the Virtual Machine Manager server.
####################################################################
# Substitute the name of your VMM server and domain in this command:
Get-VMMServer -ComputerName "VMMServer1.Contoso.com"

####################################################################
# Get the object that represents each managed computer which is
# not running the current version of the VMM agent software.
####################################################################
$VMMManagedComputers = Get-VMMManagedComputer | where {$_.VersionStateString -NE "Current"}

####################################################################
# When prompted, type the user name and password of an account
# with permissions to update both hosts and library servers.
####################################################################
$Creds = Get-Credential

####################################################################
# Loop through each managed computer that is not up-to-date
# and update its VMM agent software. 
####################################################################
ForEach ($Computer in $VMMManagedComputers )
{
# Upgrade the agents On the managed computers
Update-VMMManagedComputer -Credential $Creds
}