PowerCLI: Find Host Profiles and Versions in vCenter

As part of our planned upgrade to vSphere 6.7, we needed the ability to quickly scan the various vCenter Servers for host profiles that may be configured for version 5.5 or older. According to the vSphere 6.7 Release Notes, if these older host profiles are found, the vCenter pre-upgrade check will fail.

Although you can see the list of host profiles in vCenter, it doesn’t report the host profile version in vCenter 6.0 or 6.5. So, I put this script together as a way to find the host profile name, version, and description for all profiles found in that vCenter Server. As with my other scripts, the latest version can be found on my GitHub page. Here’s the script:

############################################################
# Script: find-host-profiles-in-vcenter.ps1
# Author: Doug DeFrank
# Date: 2019-07-30
#
# Purpose: Find Host Profiles (incl. versions) in vCenter.
############################################################

Write-Host `n "This script will find host profiles that are defined in vCenter and report the Name, Version, and Description of each." `n

### Define the date in the yyyyMMdd format
$date = Get-Date -format "yyyyMMdd"

### Prompt user for vCenter Server name, and connect to it
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'

### This Try/Catch statement will stop the script if a vCenter Server doesn't exist, a bad username/password is entered, etc.
Try {
    Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}

Catch {
    Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
    Exit
}

### Get all VMs in the chosen cluster
$HostProfiles = Get-VMHostProfile | Sort-Object | Select-Object Name,@{N='Version';E={$_.ExtensionData.Config.ApplyProfile.ProfileVersion}},Description

### Check to see if the host profiles variable IS NOT $null
if ($HostProfiles) {
    ### Ask the user if they want to export the results to a CSV file
    Do {
        Write-Host `n "Do you want to export the results to a CSV file?"
        Write-Host "1.) Yes"
        Write-Host "2.) No"
        $csvexportyn = Read-Host
        Switch ($csvexportyn) {

            ### If user chooses 1.) Yes, display the output and export the results to a CSV file in the same location as the script itself
            1 {
                $HostProfiles | Out-GridView
                Write-Host `n "Exporting CSV..."
                $HostProfiles | Export-CSV -path ".\$vCenterServer-host-profile-report-$date.csv" -NoTypeInformation
                $yn = $true
            }

            ### If user chooses 2.) No, display a separate window with the results, and exit the script
            2 {
                $HostProfiles | Out-GridView
                $yn = $true
            }

            ### Validate the input. If it's not a 1 or a 2, repeat the question
            default {
                Write-Host -ForegroundColor Red ">>> Invalid input. Please enter a [1] for Yes or a [2] for No."
                $yn = $false
            }
        }
    }

    ### Loop through the "Export CSV" question until a valid choice is made
    Until ($yn)
}

### If the Host Profiles variable IS $null (empty), report that no host profiles were found in the vCenter Server.
Else {
    Write-Host -ForegroundColor Yellow "No host profiles detected on vCenter $vCenterServer."
}

### Disconnect from the vCenter Server
Write-Host `n -ForegroundColor Green "Disconnecting from the vCenter Server $vCenterServer."
Disconnect-VIServer -Server $vCenterServer -confirm:$false | Out-Null

If you’re in the midst of planning your vSphere upgrade, I hope this script helps you validate that your host profiles are at the correct version needed before proceeding with your upgrade. In addition, be sure to check out this free eBook titled “Upgrading to VMware vSphere 6.7.” by David Stamen and Nigel Hickey. It’s a great resource that covers the new features in 6.7, as well as the pre-upgrade resources, step-by-step upgrade processes, and post-upgrade activities to consider when planning your next upgrade.

As always, thanks for stopping by! If you found this post helpful, feel free to share it on social media to help others who may find it useful.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s