cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
566
Views
4
Helpful
2
Replies

PowerShell- Find which vNIC Template a VLAN Group belongs to

JDMils
Level 1
Level 1

Is this possible? I've tried multiple combinations of Get-UcsFabricNetGroup & Get-UcsVnicTemplate and cannot seem to get this to work.

Both UCS commands produce results, I just can't figger out how to get the parent of a specific UCS Fabric Net Group.

I'm sure it's very simple to do?

 

1 Accepted Solution

Accepted Solutions

Brian Morrissey
Cisco Employee
Cisco Employee

Will this do what you are looking for?

$vnicTemplates = Get-UcsVnicTemplate

foreach($vnicTemplate in $vnicTemplates)
{
	Write-Host vNIC Template $vnicTemplate.Name -ForegroundColor yellow
	$vlanGroups = Get-UcsManagedObject -Dn $vnicTemplate.Dn -Hierarchy
	foreach($vlanGroup in $vlanGroups)
	{
		if($vlanGroup.OperName -like "fabric/lan/net-group*")
		{
			Write-Host `t Vlan Group: $vlanGroup.Name -ForegroundColor white
		}
	}
}

Output:

BrianMorrissey_0-1692208804856.png

UCSM View:

BrianMorrissey_1-1692208853258.png

 

View solution in original post

2 Replies 2

Brian Morrissey
Cisco Employee
Cisco Employee

Will this do what you are looking for?

$vnicTemplates = Get-UcsVnicTemplate

foreach($vnicTemplate in $vnicTemplates)
{
	Write-Host vNIC Template $vnicTemplate.Name -ForegroundColor yellow
	$vlanGroups = Get-UcsManagedObject -Dn $vnicTemplate.Dn -Hierarchy
	foreach($vlanGroup in $vlanGroups)
	{
		if($vlanGroup.OperName -like "fabric/lan/net-group*")
		{
			Write-Host `t Vlan Group: $vlanGroup.Name -ForegroundColor white
		}
	}
}

Output:

BrianMorrissey_0-1692208804856.png

UCSM View:

BrianMorrissey_1-1692208853258.png

 

Ahhhh, so unlike other systems such as vSphere, related items in UCSM are not connected to each other in any way. So it seems from your example, the only way to associate some objects to their parent object, such as vLAN Groups to vNIC Templates, is to use the DN properties of each item to ensure they are matching. VERY NICE!

Based on your fix, I've turned your humble code into a beast of an animal, one which can do the following:

You can perform the following actions:
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs.
* List respective vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vNIC Template.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vLAN Group.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vLAN.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the ID of a vLAN.

The parameters can accept the wildcard character "*" to find multiple instances.

And it works really well! I spent a lot of time on this one, and it although I'm a novice PowerShell programmer and even more novice UCSM users, I think it rocks. I would love to see what people think of it and if it can be improved.

Thanks again.

<#   
.SYNOPSIS
UCS Manager does not give the ability to track the relationship between vLAN Groups and vNIC Templates. This script will thus extract information from the connected UCS Manager regarding vNIC Templates, vLAN Groups and vLANs.
This script only works with vLANs which are present in vLAN Groups.

Version: 1.5
Written by: Julian Milano
Date Created: 18th Aug 2023

.Description
The script gathers all vNIC Templates and checks for associated vLAN Groups. Where a vLAN Group is found, the vLANs within that vLAN Group are extracted.
You can perform the following actions:
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs.
* List respective vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vNIC Template.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vLAN Group.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the name of a vLAN.
* List ALL vNIC Templates, associated vLAN Groups and respective vLANs using the ID of a vLAN.

The parameters can accept the wildcard character "*" to find multiple instances.


.PARAMETER vNICTemplateName
Optional. The name of the vNIC Template to interrogate.

.PARAMETER vLANGroupName
Optional. The name of the vLAN Group to interrogate.

.PARAMETER VLANName
Optional. The name of the vLAN to interrogate.

.PARAMETER vLANID
Optional. The ID of the vLAN to interrogate.

.PARAMETER ShowVLANs
Optional. By default, vLANs assocaited with vLAN Groups extracted from UCSM are not displayed.
This parameter will display the vLAN IDs and vLAN Names.

.PARAMETER ShowEmpty
Optional. By default, vNIC Templates with no associated vLAN Groups are not displayed.
This parameter will display the vNIC Template & DN.

.EXAMPLE
.\Get-UCScNICTemplate.ps1

Run the script on the currently connected UCS Manager displaying all vNIC Templates which have a vLAN Group associated.
The results show the vNIC Template name & vLAN Group Name.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -ShowVLANs

Run the script on the currently connected UCS Manager displaying all vNIC Templates which have a vLAN Group associated.
The results show the vNIC Template name, vLAN Group Name & vLANs in the respective vLAN Group.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -ShowEmpty
.EXAMPLE
Run the script on the currently connected UCS Manager displaying all vNIC Templates.
The results show the vNIC Template name and vLAN Group Name where one exists.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -vNICTemplateName "PFE-ESXI-A"

Run the script on the currently connected UCS Manager displaying the vNIC Template "PFE-ESXI-A".
The results show the vNIC Template name and vLAN Group Name where one exists.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -vLANGroupName "VGrp_TLS"

Run the script on the currently connected UCS Manager displaying all vNIC Templates which contain the vLAN Group "VGrp_TLS".
The results show the vNIC Template name and vLAN Group Name where one exists.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -VLANName "res_com_drtestprod_1902"

Run the script on the currently connected UCS Manager displaying all vNIC Templates which contain the vLAN name "res_com_drtestprod_1902".
The results show the vNIC Template name and vLAN Group Name where one exists.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -vLANID 1902

Run the script on the currently connected UCS Manager displaying all vNIC Templates which contain the vLAN ID "1902".
The results show the vNIC Template name and vLAN Group Name where one exists.
------------------------------------------------------------------------------------------------------------------------------
.EXAMPLE
.\Get-UCScNICTemplate.ps1 -VLANName "res_com_drtestprod_1902" -ShowVLANs

Run the script on the currently connected UCS Manager displaying all vNIC Templates which contain the vLAN Name "res_com_drtestprod_1902".
The results show the vNIC Template name, vLAN Group Name and all vLANs in the respective vLAN Group where one exists.

.INPUTS
None

.OUTPUTS
Results are exported to a CSV file in the same folder from which the script is run, with the following filenam convention:
"<UCS_Name>_vNICTemplate_Audit_<yyyy-MM-dd_HH_mm_ss>.csv"

.NOTES

.LINK
https://confluence.interactive.com.au/display/TEAM/SCRUCS0008-+Get+UCS+vNIC+Templates+with+vLAN+Groups+with+vLANs
#>

Param (
        [Parameter(Mandatory=$False)]
        [Switch]$ShowEmpty,

        [Parameter(Mandatory=$False)]
        [Switch]$ShowVLANs,

        [Parameter(Mandatory=$False)]
        [String]$vNICTemplateName,

        [Parameter(Mandatory=$False)]
        [String]$vLANGroupName,

        [Parameter(Mandatory=$False)]
        [String]$VLANName,

        [Parameter(Mandatory=$False)]
        [String]$vLANID
    )

Write-Host "Script Starting." -ForegroundColor Green

# Any parameters not specifically set, set them to the wildcard "*".
If (!$vNICTemplateName)
    {
    $vNICTemplateName = "*"
    }

If (!$vLANGroupName)
    {
    $vLANGroupName = "*"
    }

If (!$VLANName)
    {
    $VLANName = "*"
    }

If (!$vLANID)
    {
    $vLANID = "*"
    }

[boolean]$bShowEmpty = $False
If ($ShowEmpty.IsPresent)
    {
    [boolean]$bShowEmpty = $True
    }

# Check if connected to UCS Manager.
If (!$DefaultUcs)
    {
    # Connection is not available
    Write-Host "ERROR: No connection exists to UCS Manager." -foreground Red
    Write-Host "Please use the Connect-UCS command to connect to UCS Manager." -ForegroundColor Red
    Exit
    }

$UCSInventory = @()
# Get the vNIC Templates.
# If no template name is specified in the parameter $vNICTemplateName, then all templates are returned [$vNICTemplateName = '*'].
$vnicTemplates = Get-UcsVnicTemplate | Where-Object {$_.Name -like $vNICTemplateName}
# Loop through each vNIC Template.
ForEach($vnicTemplate in $vnicTemplates)
    {
    $vNIC_Tempalte_Name = $vnicTemplate.Name
    $vNIC_Template_DN = $vNICTemplate.DN
    $Item = New-Object PSObject

    # Get all VLAN Groups which have the same DN as the vNIC Template. These vLAN groups are not just the ones which contain vLANs!
	$vlanGroups = Get-UcsManagedObject -Dn $vnicTemplate.Dn -Hierarchy
    # This counter is used to trigger the writing of the vNIC Template name & DN
    # When only the first group is found in the vNIC Template.
    $vLANGroupFound = 0
    # Cycle through all VLAN Groups.
	ForEach($vlanGroup in $vlanGroups)
	    {
        $ThisvLANGroupName = $vlanGroup.Name
        # Check if the VLAN Group $vlanGroups contains actual vLAN Groups.
        # Only retrieve the specified vLAN Group if one is specified in the parameter, else all vLAN Groups are returned [$vLANGroupName = '*'].
		If( ($vlanGroup.OperName -like "fabric/lan/net-group*") -and ($ThisvLANGroupName -like $vLANGroupName) )
		    {
            # Found a valid vLAN Group.
            # Get all vLANS in this vLAN Group.
            # If a vLAN Name is specified in the parameter, only retrieve the vLANs with that name, else all vLANs  are returned [$VLANName = '*'].
            
            # Depending on what combination of vLAN Name & vLAN ID is provided, the search criteria is adjusted.
            If ( ($vLANID -eq "*") -and ($vLANName -eq "*") )
                {
                $tmpvLANName = "*"
                }

            If ( ($vLANID -eq "*") -and ($vLANName -ne "*") )
                {
                $tmpvLANName = $vLANName
                }

            If ( ($vLANID -ne "*") -and ($vLANName -eq "*") )
                {
                # Convert vLAN ID to vLAN Name.
                $tmpvLANName = (Get-UCSvLAN -id $VLANID).Name 
                }

            If ( ($vLANID -ne "*") -and ($vLANName -ne "*") )
                {
                $tmpvLANName = $vLANName 
                }

            # Retrieve the Fabric Pool vLANs- these do not have vLAN IDs!
            $VLANsInVLANGroup = Get-UcsFabricNetGroup -Name $ThisvLANGroupName | Get-UcsFabricPooledVlan | Where-Object {$_.Name -like $tmpvLANName} 
            # $VLANsInVLANGroup will be >0 if the vLAN was found in the vLAN Group.
            If ($VLANsInVLANGroup.Count -gt 0)
                {
                # When set to only "1", this triggers the vNIC Template name to be displayed.
                $vLANGroupFound += 1
                }
            # If this is the first vLAN Group enumerated and the vLAN is found in the vLAN list.
            If ( ($vLANGroupFound -eq 1) -and ($VLANsInVLANGroup.Count -gt 0) )
                {
                # Write the vNIC Template details to screen.
                Write-Host vNIC Template $vNIC_Tempalte_Name -ForegroundColor yellow -NoNewLine
                Write-Host "`t [$vNIC_Template_DN]" -ForegroundColor Gray
                }
            # Check if a specific vLANID has been specified as the parameter.
            If ($VLANID -ne '*')
                {
                # A specific vLANID has been specified. Only show that vLAN.
                $oUCSVLAN = Get-UCSvLAN -id $VLANID
                # Compare the vLAN name and check if it is in the collection of vLANs from the extracted vLAN Group.
                $VLAN_IN_VLANGROUP = $VLANsInVLANGroup.Name -contains $($oUCSVLAN.Name)
                # Does the vLAN Group contains the vLAN Name?
                If ($VLAN_IN_VLANGROUP)
                    {
                    # vLAN Name was found in the vLAN collection of the vLAN Group.
                    # Show the vLAN Group name and the vLAN details.
                    $vLAN_Group_Name = $ThisvLANGroupName
                    $vLAN_Name = $oUCSVLAN.Name
                    $vLAN_ID = $oUCSVLAN.ID
                    # Did the user specified a specific vLAN ID, and the vLAN is in the vLAN Group?
                    # If ($VLANsInVLANGroup.Count -gt 0)
                    #    {
                        # Yes. Display the Group Name.
                        Write-Host "`t Vlan Group: $($ThisvLANGroupName)" -ForegroundColor white
                        If ($ShowVLANs.IsPresent)
                            {
                            Write-Host "`t `t VlanID: [$($oUCSVLAN.ID)] vLAN_Name: [$($oUCSVLAN.Name)]" -ForegroundColor Cyan
                            }
                    #    }
                    $Item | Add-Member -type NoteProperty -Name 'vNIC_Tempalte_Name' -Value $vNIC_Tempalte_Name 
                    $Item | Add-Member -type NoteProperty -Name 'vNIC_Template_DN' -Value $vNIC_Template_DN
                    $Item | Add-Member -type NoteProperty -Name 'vLAN_Group_Name' -Value $vLAN_Group_Name
                    $Item | Add-Member -type NoteProperty -Name 'vLAN_Name' -Value $vLAN_Name
                    $Item | Add-Member -type NoteProperty -Name 'vLAN_ID' -Value $vLAN_ID
                    $UCSInventory += $Item
                    }
                Else
                    {
                    # No vLANs to show.
                    }
                }
            Else
                {
                # The VLAN ID is "*".
                # Was the vLAN found in the vLAN Group?
                If ($VLANsInVLANGroup.Count -gt 0)
                    {
                    # Yes, vLAN was found in the vLAN Group.
                    $vLAN_Group_Name = $ThisvLANGroupName

                    Write-Host "`t Vlan Group: $ThisvLANGroupName" -ForegroundColor white
                    # Check if the user wants to show all vLANs as well.
                    ForEach($VLAN in $VLANsInVLANGroup)
                        {
                        # We have the vLAN name- get the vLAN Object & properties.
                        $VLAN_Details = Get-UCSVlan -Name $($VLAN.Name)
                        $vLAN_Name = $VLAN_Details.Name
                        $vLAN_ID = $VLAN_Details.ID
                        
                        $Item = New-Object PSObject
                        $Item | Add-Member -type NoteProperty -Name 'vNIC_Tempalte_Name' -Value $vNIC_Tempalte_Name 
                        $Item | Add-Member -type NoteProperty -Name 'vNIC_Template_DN' -Value $vNIC_Template_DN
                        $Item | Add-Member -type NoteProperty -Name 'vLAN_Group_Name' -Value $vLAN_Group_Name
                        $Item | Add-Member -type NoteProperty -Name 'vLAN_Name' -Value $vLAN_Name
                        $Item | Add-Member -type NoteProperty -Name 'vLAN_ID' -Value $vLAN_ID
                        $UCSInventory += $Item

                        If ($ShowVLANs.IsPresent)
                            {
                            Write-Host "`t `t VlanID: [$($VLAN_Details.ID)] vLAN_Name: [$($VLAN_Details.Name)]" -ForegroundColor Cyan
                            }
                        }
                    }
                }          
		    }
	    }
    If (!$vLANGroupFound)
        {
        If ( ($vLANGroupFound -eq 0) -and $bShowEmpty)
            {
            Write-Host vNIC Template $vNIC_Tempalte_Name -ForegroundColor yellow -NoNewLine
            Write-Host "`t [$vNIC_Template_DN]" -ForegroundColor Gray
            $Item = New-Object PSObject
            $Item | Add-Member -type NoteProperty -Name 'vNIC_Tempalte_Name' -Value $vNIC_Tempalte_Name 
            $Item | Add-Member -type NoteProperty -Name 'vNIC_Template_DN' -Value $vNIC_Template_DN
            $Item | Add-Member -type NoteProperty -Name 'vLAN_Group_Name' -Value "NA"
            $Item | Add-Member -type NoteProperty -Name 'vLAN_Name' -Value "NA"
            $Item | Add-Member -type NoteProperty -Name 'vLAN_ID' -Value "NA"
            $UCSInventory += $Item
            }
        }
    }

# Write the collected data to CSV file.
$CurrPath = (Get-Location).path
$DateTimeSerial = Get-Date -Format "yyyy-MM-dd_HH_mm_ss"
$Filename = "$CurrPath\$($DefaultUCS.UCS)_vNICTemplate_Audit_$($DateTimeSerial).csv"
Write-Host ""
Write-Host "Exporting results to log file: [$Filename]" -ForegroundColor Yellow
$UCSInventory | Export-CSV -Path $Filename -NoTypeInformation -UseCulture

Write-Host "Script Finished." -ForegroundColor Green

Review Cisco Networking for a $25 gift card