Hello,
I am working on a script which is capable of querying a vCenter Server for a list of VMs which have at least one NIC attached but not set to connect at power on. The output should also return each respective VM's current power state and a few fields related to NIC attributes such as interface name, type, and current connection state. In my case, the data is stored within a variable and is exported to CSV or presented inside a PowerShell Grid window (dependent on user selection not included in the snippet below).
Specifically, I have the following line which successfully (albeit slowly) queries my vCenter Server and returns everything except the present connection state of the NIC:
$Report = Get-VM | Sort-Object name | Get-NetworkAdapter | Where-object {$_.ConnectionState.StartConnected -ne "True"} | Select-Object @{N="VM Name";E="Parent"},@{N="Power State";E={(Get-VM $_.Parent | Select-Object -Property PowerState).PowerState}},@{N="Interface";E="Name"},@{N="Type";E="Type"},@{N="Connect on Boot";E="ConnectionState"}
While I suspect there is opportunity for optimization in querying the respective VM's power state (open to suggestions!), I am struggling with returning the NIC Connection State in the form of "Connected" or "NotConnected". The "ConnectionState" is presently returned as "NotConnected, GuestControl, NoStartConnected" - but I just want to know if the state is connected or not. I tried replacing "ConnectionState" with "ConnectionState.Connected", but the output is blank. I cannot help but feel as though I'm missing something silly, but I'm kind of stumped.
I have reviewed the community topic below, but haven't found the right combination or arrangement of bits to retrieve the information I'm looking for:
https://community.broadcom.com/vmware-cloud-foundation/discussion/powercli-script-to-find-disconnected-vms#bm3529ee20-d7a0-476b-89d1-e7cc8b48e9c4
Any help would be greatly appreciated! Thank you!