PowerCLI one liner (find disconnected VM’s)

February 17, 2011 — 4 Comments

I recently found myself investigating a problem with a distributed virtual switch after some unplanned downtime of a vCenter due to a power cut. The problem with the DVS was quickly resolved after bringing the vCenter back up but I had a number of VM’s that were powered up but not connected to the network.

With my new found love for powershell I whipped up a one liner to report on the VM’s that were disconnected from the network.

Get-NetworkAdapter (get-vm | where {$_.powerstate -eq “poweredon”}) | Where { $_.connectionstate.connected -eq “$F” } | select parent, connectionstate

One part that confused me was the Where { $_.connectionstate.connected -eq “$F” } I found if I wrote $False it would give me the results I wanted but $F did, thats one for me to ask the PowerCLI guru’s.

As there were only a couple of VM’s with NICs disconnected I went and set them online manually, but I could have easily done that with PowerCLI as well.

UPDATE

As Alan kindly explained in the comments below

$flase is a pre-defined variablem if you type $false at the PowerShell prompt you will see it return False but $f is just an empty variable, if you type $f at the cmd prompt it will return anything, I normally use $null just as a default so people know it means nothing.

So the script to make more sense when reading through it could be changed to look like this

Get-NetworkAdapter (get-vm | where {$_.powerstate -eq “poweredon”}) | Where { $_.connectionstate.connected -eq “$null” } | select parent, connectionstate

Many thanks to Alan once again for putting me on the correct track with PowerCLI.

4 responses to PowerCLI one liner (find disconnected VM’s)

  1. 

    Barry,

    $flase is a pre-defined variablem if you type $false at the PowerShell prompt you will see it return False but $f is just an empty variable, if you type $f at the cmd prompt it will return anything, I normally use $null just as a default so people know it means nothing.

  2. 

    Get-VM | Get-Networkadapter | Select-Object Parent,ConnectionState

  3. 

    awesome!

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 )

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