VMware Cloud Community
EddieMashayev
Contributor
Contributor
Jump to solution

Detect VM with 4 network adapters and same NetworkName

Hi All,

I’m trying to write a script which will find all servers with 4 network adapters and their 3rd and 4th adapter have the same NetworkName(VLAN).

I want to have output with:

Server name

NetworkName (the name of the network which is the same on the 3rd and 4th NICs).

I wrote something, but I can't find if 3,4 NIC NetworkName is the same:

$ArrayVMS4Nics = Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, @{n="NumNICs"; e={(Get-NetworkAdapter -VM $_ | Measure-Object).Count}} | ?{$_.NumNICs -eq 4} | Select Name -ExpandProperty Name 

foreach($vm in $ArrayVMS4Nics)

{

  Get-VM $vm | Get-NetworkAdapter | select NetworkName, Parent

}

Thanks for any help or advice.

EddieM

1 Solution

Accepted Solutions
jpsider
Expert
Expert
Jump to solution

In your 'foreach' loop, you can create a 2nd array of Network adapters.

$myVmNetworkAdapters = Get-VM $vm | Get-NetworkAdapter | select NetworkName, Parent

And in theory you could write an if statement. You have the option to set additional variable names based on the index in the array or just specify the index in the if statement.

If ($myVmNetworkAdapters[2].NetworkName = $myVmNetworkAdapters[3].NetworkName) {

     Do something (not sure what you want to do here)

}

Now I didn't test the syntax, but hopefully you have the idea.

View solution in original post

4 Replies
jpsider
Expert
Expert
Jump to solution

In your 'foreach' loop, you can create a 2nd array of Network adapters.

$myVmNetworkAdapters = Get-VM $vm | Get-NetworkAdapter | select NetworkName, Parent

And in theory you could write an if statement. You have the option to set additional variable names based on the index in the array or just specify the index in the if statement.

If ($myVmNetworkAdapters[2].NetworkName = $myVmNetworkAdapters[3].NetworkName) {

     Do something (not sure what you want to do here)

}

Now I didn't test the syntax, but hopefully you have the idea.

ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

Please try

$VMwith4NetworkAdapters = get-vm | where {($_.NetworkAdapters | measure-object).count -eq 4}

$VMwith4NetworkAdapters | where {$_.NetworkAdapters[2].NetworkName -eq $_.NetworkAdapters[3].NetworkName}|Select-Object Name,@{name="NetworkName";Expression={$_.NetworkAdapters[2].NetworkName}}

Or as a "long" one liner

get-vm | where {($_.NetworkAdapters | measure-object).count -eq 4} | where {$_.NetworkAdapters[2].NetworkName -eq $_.NetworkAdapters[3].NetworkName}|Select-Object Name,@{name="NetworkName";Expression={$_.NetworkAdapters[2].NetworkName}}

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
EddieMashayev
Contributor
Contributor
Jump to solution

Thanks - It helped me a lot Smiley Happy

0 Kudos
EddieMashayev
Contributor
Contributor
Jump to solution

Thanks!!

0 Kudos