0

Comparing VLANs in my VMware environment to the VLANs in my UCS environment. I first read all the VLANs available in VMware into $vCenterVLANList, which returns an object looking like this:

Portgroup VLANID
--------  ---
MgmtVLAN  5
ProdVLAN  10
QVLAN     15

(real list of course much longer)

Then in my UCS system I want to search by VLAN ID for all VLANs with this ID. A VLAN ID can exist multiple times but under different name:

Name         ID
--------     ---
MgmtVLAN     5
ProdVLANDC1  10
ProdVLANDC2  10
QVLAN        15
TESTVLAN     20

The above list is obtained using Get-UCSCentralVLANs.

What I would normaly do now is create a foreach loop on $vCenterVLANList and then for each portgroup, search the VLAN ID in Get-UCSCentralVLANs and create a new object to which I parse portgroup, VLANID and the UCSCentral VLAN Name.

Trying to learn to write some better PowerShell and so I was hoping this can also be done in a one liner that would result in:

Portgroup VLANID UCSVLAN
--------  ---    -----
MgmtVLAN  5      MgmtVLAN
ProdVLAN  10     ProdVLANDC1
ProdVLAN  10     ProdVLANDC2
QVLAN     15     QVLAN

So I then would be building a foreach loop like:

$MatchedVLANs = @()
foreach ($VLAN in $vCenterVLANList) {
    $UCSVLANs = Get-UCSCentralVLANs | Where-Object( $_.ID -eq $VLAN.ID)
    foreach ($FoundVLAN in $UCSVLANs) {
        $row = "" | select Name, ID, UCSName
        $row.Name = $VLAN.Name
        $row.ID = $VLAN.ID
        $row.UCSName = $FoundVLAN.Name
        $MatchedVLANs += $row
    }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Gabrie
  • 537
  • 1
  • 5
  • 15
  • 1
    It's been said before but "one-liners" aren't "better". In my opinion, "better" code is robust and readable. Using on-liners undermines both of these :) – I.T Delinquent Aug 13 '19 at 10:21
  • Ah I see, I thought it would be more efficient because of fewer calls. – Gabrie Aug 13 '19 at 10:22
  • Could you update your question with what you are currently using? :) If you are calling the `Get-UCSCentralVLANS` command in each of your loops then that would be inefficient. – I.T Delinquent Aug 13 '19 at 10:23

0 Answers0