VMware Cloud Community
munizaba
Contributor
Contributor
Jump to solution

Network Device Status

Hi All,

At the moment I’m having a problem with my Virtual network. I have run-out of the ports on Virtual switch

and now some of the VM network cards are disconnected.

Could you please help me with script to show all VM(s) where network is not connected

as per attached image?

Thanks

0 Kudos
1 Solution

Accepted Solutions
halr9000
Commander
Commander
Jump to solution

$ConnectedExp = @{ Name = "Connected"; Expr = { ($_ | Get-NetworkAdapter).ConnectionState.Connected } }
get-vm | select name, $ConnectedExp

Or, just to get the disconnected ones:

get-vm | select name, $ConnectedExp | where-object { !$_.Connected }

Added bonus:

Get-VirtualSwitch -vmhost Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000

View solution in original post

0 Kudos
8 Replies
halr9000
Commander
Commander
Jump to solution

$ConnectedExp = @{ Name = "Connected"; Expr = { ($_ | Get-NetworkAdapter).ConnectionState.Connected } }
get-vm | select name, $ConnectedExp

Or, just to get the disconnected ones:

get-vm | select name, $ConnectedExp | where-object { !$_.Connected }

Added bonus:

Get-VirtualSwitch -vmhost Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
munizaba
Contributor
Contributor
Jump to solution

Yep is working fine. How are you going with your book?

Only problem with script below, it is showing me all VM(s) with two or more network cards but I can filter them by using (findstr False)

get-vm | select name, $ConnectedExp | where-object { !$_.Connected }

Thanks,

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Hi,

I am getting this error message when i try to tun these scripts

C:\> get-vm | select name, $ConnectedExp | where-object { !$_

.Connected }

Select-Object : Null parameter. Expecting one of the following types: {System.S

tring, System.Management.Automation.ScriptBlock}.

At line:1 char:16

+ get-vm | select <<<< name, $ConnectedExp | where-object { !$_.Connected }

+ CategoryInfo : InvalidArgument: (Smiley Happy , NotSupport

edException

+ FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co

mmands.SelectObjectCommand

0 Kudos
AS80
Contributor
Contributor
Jump to solution

C:\> get-vm | select name, $ConnectedExp | where-object { !$_

.Connected }

Select-Object : Null parameter. Expecting one of the following types: {System.S

tring, System.Management.Automation.ScriptBlock}.

At line:1 char:16

+ get-vm | select <<<< name, $ConnectedExp | where-object { !$_.Connected }

+ CategoryInfo : InvalidArgument: (Smiley Happy , NotSupport

edException

+ FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co

mmands.SelectObjectCommand

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if you didn't execute the line

$ConnectedExp = @{ Name = "Connected"; Expr = { ($_ | Get-NetworkAdapter).ConnectionState.Connected } }

first. In that line you state how $ConnectedExp is obtained.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Hi Luc,

Can you please tell how can I find out.. how can i find out the disconnected network adapters for VM's??.. the below script doesn't show me any thing.

Can you please tell me how can i find out which VM's are diconnected..

$ConnectedExp = @{ Name = "Connected"; Expr = { ($_ | Get-NetworkAdapter).ConnectionState.Connected } }

Thanks for your help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The two lines Hal gave are ok, but you have to execute them both.

First you store in the $ConnectedExp variable how the property is calculated.

Then you use it in the Select cmdlet.

$ConnectedExp = @{ Name = "Connected"; Expr = { ($_ | Get-NetworkAdapter).ConnectionState.Connected } }
get-vm | select name, $ConnectedExp | where-object { !$_.Connected }

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Thanks work great.. appreciate your help always

0 Kudos