Using PowerShell to bulk update Hyper-V Agents

If you use System Center Virtual Machine Manager to manage your Hyper-V hosts, you will likely find that the Virtual Machine Manager agent that runs on the individual Hyper-V host servers will sometimes become outdated. You can easily update a host’s agent through the Virtual Machine Manager administration console. All you have to do is to select the Fabric workspace, right-click on the Hyper-V host that you want to update, and then select the Update Agent command from the shortcut menu. You can see what this looks like in the image below.

powershell hyper-v

PowerShell and Hyper-V: Making management easier

As simple as this technique might be, it will likely be more effective to use PowerShell in environments with a lot of managed Hyper-V hosts. Not only does PowerShell simplify the task of bulk Hyper-V host management, but you can script the entire operation. That means that you can set up a scheduled job to keep your host agents up to date automatically.

With that said, I want to show you how to create a PowerShell script that can update all of the agents on managed Hyper-V hosts within a specific host group.

The first thing you will need is a list of the host groups defined within Virtual Machine Manager. While this isn’t technically a requirement, it is helpful to look at a list of host group names because you will need the exact name of the host group whose Hyper-V hosts you want to update. To see a list of the host groups that have been defined within VMM, open the Virtual Machine Manager Command Shell and enter the following command:

Get-SCVMHostGroup | Select-Object Name


You can use the Get-SCVMHostGroup cmdlet to see a list of the host groups that have been created within VMM.

Once you know the name of the host group you want to target, the next step is to create a list of the Hyper-V hosts within the host group. This requires you to use three commands. The first of these commands is:

$HostGroup = Get-SCVMHostGroup -Name <host group name> -VMMServer <vmm server name>

This command retrieves the host group and writes it to a variable named $HostGroup. You will need to supply the host group name and the Virtual Machine Manager server’s fully qualified domain name. In my organization, for instance, this command looks like this:

$HostGroup = Get-SCVMHostGroup -Name Hyper-V -VMMServer vmm.mgmt.com

Next, you will need to set up a variable named Hosts that stores the hosts within the host group. This can be accomplished using this line of code:

$Hosts = Get-SCVMHost -VMHostGroup $HostGroup

The last step in this process is to take the host list and turn it into plain text. This command extracts the names of the hosts within the host group and writes those names to a variable named $HostList.

$HostList = $Hosts.Name

The image below shows what these commands do.


Once you have created a list of the hosts within your host group, you can create a simple ForEach loop that updates the agent on each host. Before I show you how to create this loop, you may be wondering why you should bother going through all of this work when you could just as easily hard code the list of hosts or read the host list from a CSV file. The reason is that virtualized environments tend to be highly dynamic. The method that I am using will always update the hosts that currently reside within the specified host group without the need for manually updating a list of hosts.

So with that said, the next step in the process is to create a variable called $Cred and map it to the Get-Credential cmdlet. This variable will provide the permissions required to update the agents running on the hosts.

Next, we need to create a ForEach loop that steps through the servers within the list of hosts, updating each one in turn. As you may recall, the list of hosts is contained in a variable named HostList. As such, we can create a statement such as ForEach ($Server in $HostList). In this loop, the variable $Server will represent the host that is currently being updated.

The next command creates a variable named $VMMManagedHost and sets that variable equal to the server that the loop is currently set to update. The reason why we are using this line of code ($VMMManagedHost = Get-VMManagedComputer -ComputerName $Server) as opposed to using the $Server variable in its raw form is that we have to reference the host in a way that the update process can understand. The update process is performed by using the Update-SCVMMManagedComputer cmdlet and referencing the $VMMManagedHost and the $Cred variable. Here is what that section of code looks like:

$Cred = Get-Credential
ForEach ($Server in $HostList){
$VMMManagedHost = Get-VMManagedComputer -ComputerName $Server
Update-SCVMManagedComputer -VMManagedComputer $VMMManagedHost -Credential $Cred
}

Here is what the script looks like in its entirety:

$Cred=Get-Credential
$HostGroup = Get-SCVMHostGroup -Name <host group name> -VMMServer <vmm server name>
$Hosts = Get-SCVMHost -VMHostGroup $HostGroup
$HostList = $Hosts.Name
ForEach ($Server in $HostList){
$VMMManagedHost = Get-VMManagedComputer -ComputerName $Server
Update-SCVMManagedComputer -VMManagedComputer $VMMManagedHost -Credential $Cred
}

You can see the script’s output in the image below.

powershell hyper-v

Virtual Machine Manager may lose connectivity — but don’t worry

One last thing you need to know about running this script is that it can cause VMM to momentarily lose connectivity to your Hyper-V servers. That means that if you are remotely connected to VMM running within a virtual machine on a host that is being updated, you might see the virtual machine session disconnect for a few minutes. However, this does not mean that the host or the virtual machine has rebooted. It only means that Virtual Machine Manager loses connectivity while the agent is being updated.

Featured image: Shutterstock

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top