Getting Active Brocade Fiber Switch Aliases via PowerShell

A while back, I posted a quick script to create commands for 1:1 Zoning on a Brocade Fiber Switch.  I was recently helping someone go through that exact process on a set of switches that had a lot of aliases already defined on them.  Their challenge was that they weren't sure which aliases were for their current SAN vs. a retired SAN.  Rather than just creating zones for both SANs, I decided to put together a quick script that would scrape their current Aliases and check which ones have active WWNs currently in the system.  They could then use this information to prune the Aliases that are no longer needed, in addition to only creating the required zones for our project.

In order to do this task, I had to do some quick string parsing into PowerShell objects.  Good thing I already know how to do that ;)  So, I put together this script which does two things:
1) It parses the Brocade Fiber Switch's configuration to look for any Aliases
2) It checks the WWNs for those Aliases against the currently logged in WWNs to report only the active WWNs

The script is pretty straight-forward.  It starts by using plink to query the Brocade switch for a its current configuration (via the aliShow command).  Once it has that, it goes through the output line by line, looking for lines that being with "Alias" (after removing any preceding spaces).  When it finds one of those lines, it stores its content in the $alias variable, then checks the next line to see if it begins with a WWN (after trimming).  It checks that via a regular expression that looks for a pattern that starts with 2 hex number characters (0-9 or a-f), followed by a colon, followed by 2 more hex number characters and then another colon.  If it finds a WWN on that string, it appends that to the $alias variable (with a space) and then checks the next line.  There should be no more WWNs for that alias, but it keeps checking and appending until it finds no more WWNs.

Once the script has discovered a complete Alias entry, it does some string manipulation on the $alias variable to make it easier to work with in PowerShell.  Most importantly, it uses a regular expression to replace all instances of 1 or more white-space characters with a single space.  Once that's done, it trims the string and stores it in the $aliases array.  Once that's done, the script uses ConvertFrom-CSV to convert my array of strings into an array of objects, using the space character as a delimiter.

At that point, depending on the state of the -ReportAll switch, the script will either return its output or will move on and check the status of each of those Aliases.  It checks their status by using the nsshow command, which shows (amongst other things) the active WWNs on the switch.  For each Alias discovered in the first section, it checks the nsshow results to see if that WWN is represented.  If it is, that Alias gets added to the final report.  Once it's all said and done, it returns that final report, which is all of the Aliases that are currently active on the switch.

How do I use it?  I just call .\get-brocadeAliases.ps1 -user <username> -switch <switchAddress> -password <password> [-reportAll].  Once again, this worked for me in my situation, but that's no guarantee that it'll work for you in yours.  This script is supplied as-is and for educational purposes... please test any script that you find on the internet thoroughly before even thinking about running it in production.

Visit GitHub for any updated versions of this get-BrocadeAliases.ps1 script.

Comments

Popular posts from this blog

Clone a Standard vSwitch from one ESXi Host to Another

PowerShell Sorting by Multiple Columns

Deleting Orphaned (AKA Zombie) VMDK Files