Using vSphere Tags to Check for Pending Reboot State

You may have seen my blog post on the Test-PendingReboot PowerShell module.  I’ve been using it a lot lately and had the thought to use vSphere Tags to check pending reboot state.  In my environment, we normally check for pending reboot states before patching.  If a server is in a pending reboot state, it won’t receive the patches from SCCM, etc.  We have 3 different groups we deploy patches to.  Staging, Production, and Global (servers outside the US).

First, I created a Patch Category called PATCHING.


New-TagCategory -Name "Patching"

Now, I’m able to create my Tags.

New-Tag -Category "Patching" -Name "Staging"
New-Tag -Category "Patching" -Name "Prod"
New-Tag -Category "Patching" -Name "Global"

Now, since all of my vCenters are linked via Enhanced Link Mode, I only had to create this category and these tags in one vCenter.  They are automatically available on all of my vCenters after that.  Pretty cool!

Then, I opened PowerShell and connected to all of my vCenter Servers.

Connect-VIServer vCenter1, vCenter2, vCenter3

If you get an error, or it only connects to ONE of the 3, you’ll need to set the PowerCLI Configuration to support connecting to multiple vCenters.

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers

Next, I had to assign the tags to the VMs. Since I have different datastore for each group of servers (SRM, PROD, STAGING, etc) I was able to easily apply the tags via datastore clusters.

Get-DatastoreCluster | Where {$_.Name -match "Staging"} | Get-VM | New-TagAssignment "Prod"

Now, I’m able to query the VMs via vSphere Tag and check their Pending Reboot Status!

BUT, we want to make sure we only run this against WINDOWS VMs and VMs that are running!  So I added a where statement to filter for that.  Lastly, we only want to report on VMs that are IN the Pending Reboot state, so I filter for that as well.

Get-VM -Tag "Prod" | Where {$_.Guest -match "win" -and $_.PowerState -eq "PoweredOn"} | Sort Name | Test-PendingReboot | Where {$_.IsRebootPending -eq "True"}

Here are what the results look like:

ComputerName                     IsRebootPending
————                                ————–
VM1                                       True
VM2                                       True
VM3                                       True

From that, I’m able to write a script to give me a report of all servers in a pending reboot state by Tag.

#####################################################
#
# Check Pending Reboot State by Tag
# Created by BLiebowitz on 11/13/18
#
#####################################################

# Build array for each Tag
Write-host "Select which Tag to use:"
Write-Host ""
Write-Host "1. Staging"
Write-Host "2. Prod"
Write-Host "3. Global"

$ITag = read-host "Select a Tag. Enter Number."

if ($ITag -match "1") {
$Tag = "Staging"
} elseif ($ITag -match "2") {
$Tag = "Prod"
} else {
$Tag = "Global"
}

write-host ""
Write-Host "You Picked: $($Tag)"
write-host ""
start-sleep -s 3

# Connect to ALL vCenter Servers
Connect-viserver vCenter1, vCente2, vCenter3

# Get a list of Powered On Windows Servers that are in a pending reboot State
Get-VM -Tag $tag | Where {$_.Guest -match "win" -and $_.PowerState -eq "PoweredOn"} | Test-PendingReboot -SkipConfigurationManagerClientCheck | Where {$_.IsRebootPending -eq "True"} | Export-csv -notypeinformation e:\ben\sccm\Pending_Reboot_List_$($Tag)_$((Get-Date).ToString('MM-dd-yyyy')).csv

# Disconnect from all vCenters
Disconnect-viserver vCenter1, vCente2, vCenter3 -Confirm:$false

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

 

Share This:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.