Parhomenkov
Contributor
Contributor

vlan network

Jump to solution

Good day! We need an output of all villans and the number of vm connected to it, etc. Thus, they understand where and in which villa there is no VM or other connection. 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  Get-VirtualSwitch -VMHost $esx -PipelineVariable sw |
  ForEach-Object -Process {
    Get-VirtualPortGroup -VirtualSwitch $sw -PipelineVariable pg |
    ForEach-Object -Process {
      New-Object -TypeName PSObject -Property ([ordered]@{
          VMHost = $esx.name
          Switch = $sw.Name
          Type = $sw.GetType().Name -replace 'Impl', ''
          Mtu = $sw.mtu
          Portgroup = $pg.Name
          VlanId = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
              } elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
                if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
                  [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object { "$($_.Start)-$($_.End)" }))
                } else {
                  $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                }
              }
            } else { $_.VlanId } }
          Uplink = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $vds.ExtensionData.Config.UplinkPortPolicy.UplinkPortName -join ','
            } else {
              'na'
            }
          }
          ConnectedAdapter = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $vds = $esx.ExtensionData.Config.Network.ProxySwitch | Where-Object { $_.DvsUuid -eq $sw.ExtensionData.Uuid }
              ($vds.UplinkPort | ForEach-Object -Process {
                  $uplink = $_
                  $pnic = $vds.Spec.Backing.PnicSpec | Where-Object { $uplink.Key -eq $_.UplinkPortKey }
                  if ($pnic) {
                    "$($uplink.Value):$($pnic.PnicDevice)"
                  } else { "$($uplink.Value):-" }
                }) -join ','
            } else {
              $sw.Nic -join ','
            }
          }
          ActiveNic = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              'na'
            } else {
              if ($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder -ne $null) {
                $pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic -join ','
              } else {
                $sw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic -join ','
              }
            }
          }
          StandByNic = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              'na'
            } else {
              if ($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder -ne $null) {
                $pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandByNic -join ','
              } else {
                $sw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandByNic -join ','
              }
            }
          }
          SwPromicious = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.AllowPromiscuous.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.AllowPromiscuous
            }
          }
          SwMacChanges = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.MacChanges.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.MacChanges
            }
          }
          SwForgedTransmits = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.ForgedTransmits.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.ForgedTransmits
            }
          }
          PgPromicious = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.AllowPromiscuous.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.AllowPromiscuous
            }
          }
          PgMacChanges = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.MacChanges.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.MacChanges
            }
          }
          PgForgedTransmits = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.ForgedTransmits.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.ForgedTransmits
            }
          }
          VM = $pg.ExtensionData.VM.Count
        })
    }
  }
}


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership

I assume you mean VLANs?
Are your portgroups on VSS, VDS or a mix of both?


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

0 Kudos
Parhomenkov
Contributor
Contributor

Да я говорю про VLAN и нужно что бы проверял все сочетания по всей инфраструктуре сети.

0 Kudos
LucD
Leadership
Leadership

You didn't answer the question about VSS and VDS.


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

0 Kudos
Parhomenkov
Contributor
Contributor

можно и обоих вариантах сразу или же нужно именно распределить то тогда VDS

0 Kudos
LucD
Leadership
Leadership

Try like this

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  Get-VirtualSwitch -VMHost $esx -PipelineVariable sw |
  ForEach-Object -Process {
    Get-VirtualPortGroup -VirtualSwitch $sw -PipelineVariable pg |
    ForEach-Object -Process {
      New-Object -TypeName PSObject -Property ([ordered]@{
          VMHost = $esx.name
          Switch = $sw.Name
          Type = $sw.GetType().Name -replace 'Impl', ''
          Mtu = $sw.mtu
          Portgroup = $pg.Name
          VlanId = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {
                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId
              } elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {
                if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {
                  [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object { "$($_.Start)-$($_.End)" }))
                } else {
                  $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                }
              }
            } else { $_.VlanId } }
          Uplink = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $vds.ExtensionData.Config.UplinkPortPolicy.UplinkPortName -join ','
            } else {
              'na'
            }
          }
          ConnectedAdapter = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $vds = $esx.ExtensionData.Config.Network.ProxySwitch | Where-Object { $_.DvsUuid -eq $sw.ExtensionData.Uuid }
              ($vds.UplinkPort | ForEach-Object -Process {
                  $uplink = $_
                  $pnic = $vds.Spec.Backing.PnicSpec | Where-Object { $uplink.Key -eq $_.UplinkPortKey }
                  if ($pnic) {
                    "$($uplink.Value):$($pnic.PnicDevice)"
                  } else { "$($uplink.Value):-" }
                }) -join ','
            } else {
              $sw.Nic -join ','
            }
          }
          ActiveNic = & {
            if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              'na'
            } else {
              if ($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder -ne $null) {
                $pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic -join ','
              } else {
                $sw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.ActiveNic -join ','
              }
            }
          }
          StandByNic = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              'na'
            } else {
              if ($pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder -ne $null) {
                $pg.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandByNic -join ','
              } else {
                $sw.ExtensionData.Spec.Policy.NicTeaming.NicOrder.StandByNic -join ','
              }
            }
          }
          SwPromicious = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.AllowPromiscuous.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.AllowPromiscuous
            }
          }
          SwMacChanges = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.MacChanges.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.MacChanges
            }
          }
          SwForgedTransmits = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $sw.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.ForgedTransmits.Value
            } else {
              $sw.ExtensionData.Spec.Policy.Security.ForgedTransmits
            }
          }
          PgPromicious = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.AllowPromiscuous.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.AllowPromiscuous
            }
          }
          PgMacChanges = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.MacChanges.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.MacChanges
            }
          }
          PgForgedTransmits = & {
            if ($pg -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {
              $pg.ExtensionData.Config.DefaultPortConfig.SecurityPolicy.ForgedTransmits.Value
            } else {
              $pg.ExtensionData.Spec.Policy.Security.ForgedTransmits
            }
          }
          VM = $pg.ExtensionData.VM.Count
        })
    }
  }
}


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

View solution in original post

0 Kudos
Parhomenkov
Contributor
Contributor

Это классный скрипт однозначно он пригодится, за него я благодарен, но вы мне кажется перемудрили. Нужен скрип который показывал столбец список всех vlan и столбец напротив который бы показывал количество подключаемых вм к этому vlan.

vlan         vm

v0900    3vm

v3140      1vm

v0389     0vm

примерно таким образом, если возможно буду блогадарен.

0 Kudos
LucD
Leadership
Leadership

Then use a Select-Object and only specify the columns you need.


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

0 Kudos
Parhomenkov
Contributor
Contributor

Нужная информация есть, просто ее очень много но данный скрипт можно считать как нужное решение данной проблемы. Спасибо Вам огромное вы сделали мой день. Обращусь непременно еще.

0 Kudos
LucD
Leadership
Leadership

Basically I did that to show that retrieving the VLANId for VSS and VDS is different.
From your original question it was not clear for me if you asked for the VLANId or not.

Besides using a Select-Object, you can also just leave out the properties you don't need.


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

0 Kudos
Parhomenkov
Contributor
Contributor
Spoiler
Spoiler
Спасибо, за вашу помощь но я в этом деле нуль и не буду сам ни чего менять. Как говорится знал бы сам сделал бы, но увы обратился к Вам еще раз спасибо.
0 Kudos