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
}
}