23 December 2009

Find Busted VMNICs

So, while we mentioned this would be a "low-volume blog", we didn't think we'd get our domain expiration notice before the first vNugglet hatched. Anyway, a problem we've run into in the past are VMNICs that suddenly have no link for whatever reason--switch upgrades, random user error, etc.

The following script will search the connected vCenter Server(s) to find any VMNICs that are actively being used in a standard vSwitch and also have no link (0Mbps).

## Script function: Lists all VMNICs that have no link and that are connected to a vSwitch (sorted by corresponding host).
## Author: vNugglets.com
## Usage: Connect to vCenter, run script interactively.


## Get all hosts and sort by name
Get-VMHost | Sort-Object -Property Name | ForEach-Object {
## (Re)Initialize array of failed VMNICs
$arrFailedVMNICsInUse = @()

## Get current host's Name, NICs, and vSwitches objects
$objCurrentHostNICs = Get-VMHostNetworkAdapter -VMHost $_
$objCurrentHostVSwitches = Get-VirtualSwitch -VMHost $_

## For each failed NIC, if it is attached to a vSwitch, add it to the failed VMNICs array
$objCurrentHostNICs | Where-Object {$_.BitRatePerSec -eq 0} | ForEach-Object {
$objCurrentFailedNIC = $_
$arrValidVMNICs = $objCurrentHostVSwitches | ForEach-Object {$_.Nic}
if ($arrValidVMNICs -match $objCurrentFailedNIC.DeviceName) {
$arrFailedVMNICsInUse += $objCurrentFailedNIC
} ## end if
} ## end inner ForEach-Object

## If there is at least one failed VMNIC, display the hostname it is associated with followed by the list of failed VMNICs
if ($arrFailedVMNICsInUse.Count -gt 0) {
$_.Name + ":"
$arrFailedVMNICsInUse | ForEach-Object {
"`t" + $_.DeviceName
} ## end ForEach-Object
Write-Host ""
} ## end if
} ## end outer ForEach-Object

That's it! Hopefully this is helpful to someone else.


*UPDATE - 19 January 2011: Updated script due to the following properties that are now deprecated.

PS C:\> .\findBustedVMnics.ps1
WARNING: 'PhysicalNic' property is obsolete. Use 'Get-VMHostNetworkAdapter' cmdlet instead.
WARNING: 'VirtualSwitch' property is obsolete. Use 'Get-VirtualSwitch' cmdlet instead.

...

08 February 2009

vNugglets Objectives

Welcome to vNugglets. Right now vNuggs is comprised of two IT masters that needed a place to share some scripts/howto's/whatever. AC started off washing dishes at Chubby's Fish & Steak. Then during and after attending college he worked as a Systems Administrator at a small shop, followed by a Fortune 500 financial company. AC currently works at a consulting company where he specializes in VMware administration/infrastructure. D-Day started his illustrious career by slinging pizza pizzas! at Little Caesar's. Then during college he too worked at a small shop as a Systems Administrator (doing everything). D-Day currently works at the same consulting company as AC, specializing in VMware administration/automation.

This blog will contain useful (hopefully) scripts, howto's, and miscellany. While there are plenty of scripts/howto's out there, vNuggs will focus on providing well-written/documented material, not just "it works, that's good enough" efforts. This includes:
  • providing example usage/output
  • good, readable, commented code
  • trying to stay away from most aliases for the sake of the readers
pertaining to:
  • VMware (generally PowerShell)
  • Sys Admin tasks
  • general automation
In focusing on these items, vNugglets.com will likely be a low-volume blog, not one that will rigorously cover current virtualization/technology news. There are excellent sources for that sort of info (and more) on our blog list.