VMware Cloud Community
shaka
Enthusiast
Enthusiast
Jump to solution

Find unused vmnic

How can I find the first vmnic with powerCLI that is not being used by any of the virtual switches on a host?  I am not concerned if it is connected or not.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this ?

It will list all the pnic and then remove the ones used in vSwitch or dvSwitch.

$nicTab = @{}
$esx = Get-VMHost MyEsx 

$esx.ExtensionData.Config.Network.Pnic | %{     $nicTab.Add($_.Key,$_.Device) } # Regular switches
$esx
.ExtensionData.Config.Network.Vswitch | %{     $_.Pnic | %{         $nicTab.Remove($_)     } } # dvSwitches
$esx.ExtensionData.Config.Network.ProxySwitch | %{     $_.Pnic | %{         $nicTab.Remove($_)     } } # What is left ?
$nicTab.GetEnumerator()


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this ?

It will list all the pnic and then remove the ones used in vSwitch or dvSwitch.

$nicTab = @{}
$esx = Get-VMHost MyEsx 

$esx.ExtensionData.Config.Network.Pnic | %{     $nicTab.Add($_.Key,$_.Device) } # Regular switches
$esx
.ExtensionData.Config.Network.Vswitch | %{     $_.Pnic | %{         $nicTab.Remove($_)     } } # dvSwitches
$esx.ExtensionData.Config.Network.ProxySwitch | %{     $_.Pnic | %{         $nicTab.Remove($_)     } } # What is left ?
$nicTab.GetEnumerator()


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

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thank you for your Scrips.

Do you have any update for this scrips?

How can I see the vSwitchs  and DVswitchs with their vmnics?

Thank you.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-View -ViewType HostSystem -PipelineVariable esx |

ForEach-Object -Process {

    $netSys = Get-View -Id $esx.ConfigManager.NetworkSystem

    foreach($vss in $netSys.NetworkInfo.Vswitch){

        foreach($nic in $vss.Pnic){

            $netSys.NetworkInfo.Pnic | where{$_.Key -eq $nic} |

            Select @{N='VMHost';E={$esx.Name}},

                @{N='Switch';E={$vss.Name}},

                @{N='SwitchType';E={'VSS'}},

                @{N='pNic';E={$_.Device}}

        }

    }

    foreach($vds in $netSys.NetworkInfo.ProxySwitch){

        foreach($nic in $vds.Spec.Backing.PnicSpec){

            $netSys.NetworkInfo.Pnic | where{$_.Device -eq $nic.PnicDevice} |

            Select @{N='VMHost';E={$esx.Name}},

                @{N='Switch';E={$vds.DvsName}},

                @{N='SwitchType';E={'VDS'}},

                @{N='pNic';E={$_.Device}}

        }

    }

}


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

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Thank you LucD.

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Thank you so much LucD.

Could you tell me what is the problem?

1.jpg

Thank you for your answer.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you might have switches with no pNICs connected.

Add a test for that.

$nicTab = @{}

$esx = Get-VMHost MyEsx


$esx.ExtensionData.Config.Network.Pnic | %{

    $nicTab.Add($_.Key,$_.Device)

}


# Regular switches

$esx.ExtensionData.Config.Network.Vswitch | %{

    $_.Pnic | %{

        if($_){

            $nicTab.Remove($_)

        }

    }

}

# dvSwitches

$esx.ExtensionData.Config.Network.ProxySwitch | %{

    $_.Pnic | %{

        if($_){

            $nicTab.Remove($_)

        }

    }

}


# What is left ?

$nicTab.GetEnumerator()


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

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Thank you so much LucD.

0 Kudos