VMware Cloud Community
jmadriaga07
Contributor
Contributor
Jump to solution

How can I Get NetworkName from VLAN ID

I would like to be able to set NetworkName in the NetworkAdapter, but obtaining this data from the "VLAN ID". In the script you should enter the "VLAN ID" set the NetworkName in the NetworkAdapter. I already have everything set up, but I only need the part to get the NetworkName from the "VLAN ID".

pastedImage_0.png

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In any case, the following will find the networkname of the PG with corresponding VlanId.
And it works for VSS and VDS.

$tgtVlanId = '20'

Get-VirtualPortGroup -PipelineVariable pg |

ForEach-Object -Process {

    $vlanId = if($pg -is [VMware.VimAutomation.ViCore.Types.V1.Host.Networking.DistributedPortGroup]){

  

         if($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){


                            $pg.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId


                        }

  

         elseif($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){


                            if($pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){


                                [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))


                            }


                            else{


                                $pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId


                            }


                        }

  

     }

     else{$pg.VlanId}

     if($vlanId -eq $tgtVlanId){

        $pg.Name

     }

}


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You mean like this?

$vlanId = '20'

Get-VirtualPortGroup | where{$_.VlanId -eq $vlanId} | select -ExpandProperty Name


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

Reply
0 Kudos
jmadriaga07
Contributor
Contributor
Jump to solution

I tried several vlanID's and it doesn't bring me data

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you using VSS or VDS?


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

LucD
Leadership
Leadership
Jump to solution

In any case, the following will find the networkname of the PG with corresponding VlanId.
And it works for VSS and VDS.

$tgtVlanId = '20'

Get-VirtualPortGroup -PipelineVariable pg |

ForEach-Object -Process {

    $vlanId = if($pg -is [VMware.VimAutomation.ViCore.Types.V1.Host.Networking.DistributedPortGroup]){

  

         if($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){


                            $pg.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId


                        }

  

         elseif($pg.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]){


                            if($pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]){


                                [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | %{"$($_.Start)-$($_.End)"}))


                            }


                            else{


                                $pg.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId


                            }


                        }

  

     }

     else{$pg.VlanId}

     if($vlanId -eq $tgtVlanId){

        $pg.Name

     }

}


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

Reply
0 Kudos
jmadriaga07
Contributor
Contributor
Jump to solution

That's right, I use VDS

Reply
0 Kudos
jmadriaga07
Contributor
Contributor
Jump to solution

It worked perfect, thank you very much

Reply
0 Kudos