Update your VMM Managed Hyper-V Host Agents with PowerShell

Hey there, the kids are finally in bed and the dog is snoring on the sofa, which means I've got some time to get this blog finished.

Firstly, I need to start this post with a caveat, a PowerShell related caveat...  I'm not a PowerShell guru!  The PowerShell gurus out there can probably smell the lack of PowerShell guru-ness emanating from my blog already... but I'm going to plough on regardless as this particular script should make upgrading the Host Agent on your VMM Managed computers much easier.

The scenario goes something like this... After you’ve deployed an Update Rollup to your VMM Server(s), you will need to update the Agent version on all VMM Managed servers (Hyper-V Hosts, Library Servers, Update Server, PXE Server etc…).  Notice in my environment, the Hosts are showing a status of "Needs Attention"...

 

Image9

 

You can update the Hosts manually from within the VMM Console by highlighting the Hosts in the Fabric view and clicking “Update Agent”...

 

Image10

 

This is fairly straightforward for small scale implementations like mine, however, if you have a large number of Hosts and/or Clusters, (or you like to do everything in PowerShell because you’re too cool for GUIs) then automating the update via PowerShell is a better option.

Let’s deconstruct the the script and walk through the steps required to identify the Agent versions in your estate and then push the latest version to all Managed Computers via PowerShell…

 

STEP 1:   Check the build version of the VMM Server, this should be the latest version and all your managed Hosts, Library Servers etc will need to be updated to the same version…

 

Update your script and replace $VMMServer with the name of your VMM Server below

 #Get the current version of the VMM Server

 $VMMServer = "VMMSERVERNAME"

 Get-SCVMMServer -ComputerName $VMMServer | ft Name, ProductVersion

 

Notice the VMM Build version in my lab is 4.0.1381.0 - which is Cumulative Update 2 for SCVMM 2016 TP5. The Managed Servers should be running the same version...

 

 Image2

 

STEP 2: List all the Managed Computers and their version numbers

 

Get a list of all Servers managed by VMM (My environment only has a few servers as I've only just built it, you should see Library Servers, Update Servers, Hosts etc...)

 

Get-SCVMMManagedComputer  | ft Name, Role, AgentVersion

Notice that some of the servers are still at a lower version:

 

Image3

 

Step 3:   Narrow down the list to include only the Servers that are not up to date with the latest version. I’m doing this by listing all Managed Computers where the agent version is not equal to the version running on my VMM Server.  You could tweak this to say “less than” rather than “not equal”, but we should never have an Agent managed by VMM that is running a version greater than the VMM Server.

 

Get list of Managed Servers that do not have the latest version of the agent compared to the VMM Server build version

 

Get-SCVMMManagedComputer | ? {$_.AgentVersion -ne "4.0.1381.0"} | ft Name, Role, AgentVersion

Image4

 

So we now have a list of targets and we can go ahead and push the Agent to them…

 

Step 4: To deploy the Agent we need to use a Run As Account that has local Admin permissions on the Managed Computers, you can find your Host Management Account in the Run As Accounts view in the Settings section of the console...

 

Image5

 

Step 5: Now it's time to update the Managed Server...

 

Copy the script below, update the VMM Server and Host Management Account variables and run it against your test environment:

 

$VMMServer = "INSERTVMMSERVERNAMEHERE"
$HostManAccount = "INSERTHOSTMANAGEMENTACCOUNTNAMEHERE"

$VMMBuild = Get-SCVMMServer -ComputerName $vmmserver
$VMMBuildVersion = $VMMBuild.ProductVersion

#Set the variables for the Run As Account
$credential = Get-SCRunAsAccount -Name $HostManAccount

#Create the list of target Servers by identifying those that are out of date and put them into an array
$managedComputers = Get-SCVMMManagedComputer | ? {$_.AgentVersion -ne $VMMBuildVersion}

#Deploy the Agent update to all servers identified as being out of date
Foreach($managedComputer in $managedComputers)
{
Update-SCVMMManagedComputer -Credential $credential -RunAsynchronously -VMMManagedComputer $managedComputer
}

After you execute the script, you can open the Jobs window in the VMM Console and you should see each Managed Host being updated…

 

Job Running…

 

Image6

 

Job Completed…  You can see that the Agent version now matches the VMM Server...

 

Image7

 

If you run the Get-SCVMMManagedComputer cmdlet again you should see that all your Agents are up to date!

 

Image8

 

Hope this saves you some time and effort!

Leigh