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.
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.
Cheers Alan, much appreciated seems obvious now 🙂 will update the post
Get-VM | Get-Networkadapter | Select-Object Parent,ConnectionState
awesome!