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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## 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.

...