PowerCLI: Quickly Look Up VM’s Boot Firmware Setting

A few months ago, I wrote two blog posts about how to find all UEFI- or BIOS-enabled virtual machines at the data center level within a vCenter Server. But what if you just want to quickly look up the boot firmware setting of a specific VM or even just a few of them?

I wrote this script as a way to quickly look up a small number of VMs to see if they might be good candidates for enabling Secure Boot or not. If you recall from those two posts, in order to enable Secure Boot, a VM needs to have virtual hardware version 13 or higher (meaning vSphere 6.5 or higher), and the VM boot firmware needs to be set to EFI.

For this script, I changed up my logic just a bit. Initially, I had a question near the end of the script that would ask if you wanted to look up another VM [Press 1 for Yes (repeat or loop through again) or choose 2 for No (Exit)]. When looking up multiple VMs, I found this to be rather annoying, so I changed things up a bit. I pulled the question altogether and replaced it logic to the VM name question near the beginning of the script. The script will automatically loop again by asking for the ‘VM name to lookup’ question until the user enters an x or X to eXit the script.

Here’s the script as it’s currently written, but be sure to check out my GitHub page for the latest revisions and edits:

############################################################
# Script: find-specific-vm-boot-firmware-setting.ps1
# Author: Doug DeFrank
# Date: 2018-10-07
# 
# Purpose: Find a specific VMs' boot firmware setting in vSphere
############################################################

Write-Host `n -ForegroundColor Yellow "This script will find and report a specific VM's boot firmware setting (BIOS or UEFI)."
Write-Host -ForegroundColor Yellow " It will continue asking for user input (a VM name) unless 'X' is entered." `name

### 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)"

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
}

### This script is designed to look up a VM's boot firmware setting.
### The script will continue asking for a VM name unless an x or X is entered in the VM name.

Do {
### Ask the user what VM to find
$vm = Read-Host -Prompt "Find the boot firmware setting for which VM [press X to exit]"
if ($vm -eq 'x' -or $vm -eq 'X') {
$yn1 = $false
}

Else {
### Lookup the VM in the environment.
### If the VM exists, report the boot firmware setting.
### However, if it doesn't exist, report that the VM couldn't be found.
$yn1 = $true
Try {
$vmlookup = Get-VM $vm -ErrorAction Stop
Write-Host `n "Found the virtual machine, $vm."
$vmlookup | Format-List Name,@{N='Firmware';E={$_.ExtensionData.Config.Firmware}}
}

Catch {
Write-Host -ForegroundColor Red -BackgroundColor Black "The VM [$vm] was not found in the vCenter Server [$vCenterServer]." `n
}
}
}

### Continue asking the "Find Which VM" question again unless the user enters an X
While ($yn1)

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

As always, thank you for stopping by!

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