I often wonder why am I doing automation because it is boring. Doing stuff manually is time consuming, inefficient but gives a sense of 'hey i am doing something'. Lol... anyway we had a VxBlock and my colleague needed to add 500+ vlans on UCS and we needed to map them to fabric-A and fabric B.
System Requirements:
Powershell 4+
Cisco Powertool Suite 2.x
The purpose of this post is to make the reader understand that how easy it is to come up with your own automation and it is easier than resisting sugar in your diet.
So let us look at the command first which does the adding of vlan
It is simple as a pimple on a dimple on my niece cheek. Okay enough of showing off of my poetic (oh i know i suck at it...) let us move on shall we!
The above command is something you can find from UCS documentation or search online. Yes, why do stuff when it is already done. Build on it not rebuild it.
$vlan here is the name of the vlan, an alphanumeric.
$vlanID is the vlan ID, an integer.
If you have seen any of my previous scripts or even seen my powershell tutorials then you will know that I like csv files to take inputs. They are simple and easy to work with. When the user runs our script we want the script to open a csv file with proper headers .
1st part of the script will display a message on the terminal and tells the user on what to do.
After this we want to define the path where the csv file will be created and it's name.
I always love to $PSScriptRoot as the path since it means that wherever the script is currently located that is the our path. So it sets the current working directory as your path. the gnu/linux shell equivalent of cwd. After that I have given the name of the file with .csv as the extension. Since we have the path and the file we now need to add headers to the file.
Do not get confused with why I am using get-process here. It is just my street smart way of adding headers (headings) to the csv file. The above '-Encoding ASCII -NoTypeInformation' is important since that enables you to open it with an excel.
Okay the start-process opens up the file but we want to wait for the user to enter the data and wait. So we will Add a Read-host command below since it pauses the script till the user enters something, in this case, just hit enter once the user is done entering the data, save the file.
Processing a csv file is damn easy. the 1st line is importing the data from the $csv file and storing all that in the $variable $csv.
2nd line is going through each line of the $csv file and creating a variable. $vlan = $($line.vlan) here means that go in the line $line where the heading or column name is vlan, take the value from that cell and store it in the variable named $vlan. The same thing is true with $vlanID. Now once we have the variables for the 1st line we want to do something with that right? so let us do it.
So once I created the variables I am using those variables to create a vlan with a vlan ID in the last line. Above that I have another line which is just to tell the user what it is doing.
Combine all the above and now you have the script to add vlans with vlan IDs to the UCS. Yes, I leave that combining part to you. Anything where you have not put some effort of your own you won't find it interesting and struggle learn to something or do something, even if it is trivial will always make you know it than just remember it.
System Requirements:
Powershell 4+
Cisco Powertool Suite 2.x
The purpose of this post is to make the reader understand that how easy it is to come up with your own automation and it is easier than resisting sugar in your diet.
So let us look at the command first which does the adding of vlan
Get-UcsLanCloud | Add-UcsVlan -DefaultNet no -Id $vlan -Name $vlanID
It is simple as a pimple on a dimple on my niece cheek. Okay enough of showing off of my poetic (oh i know i suck at it...) let us move on shall we!
The above command is something you can find from UCS documentation or search online. Yes, why do stuff when it is already done. Build on it not rebuild it.
$vlan here is the name of the vlan, an alphanumeric.
$vlanID is the vlan ID, an integer.
If you have seen any of my previous scripts or even seen my powershell tutorials then you will know that I like csv files to take inputs. They are simple and easy to work with. When the user runs our script we want the script to open a csv file with proper headers .
1st part of the script will display a message on the terminal and tells the user on what to do.
Write-Host " A CSV file will be opened (open in excel/spreadsheet) populate the values, save & close the file, Hit Enter to proceed " -ForegroundColor Blue -BackgroundColor White
After this we want to define the path where the csv file will be created and it's name.
$csv = "$PSScriptRoot/ucsAddVlan.csv"
I always love to $PSScriptRoot as the path since it means that wherever the script is currently located that is the our path. So it sets the current working directory as your path. the gnu/linux shell equivalent of cwd. After that I have given the name of the file with .csv as the extension. Since we have the path and the file we now need to add headers to the file.
get-process | Select-Object vlan,vlanID | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation
Do not get confused with why I am using get-process here. It is just my street smart way of adding headers (headings) to the csv file. The above '-Encoding ASCII -NoTypeInformation' is important since that enables you to open it with an excel.
# this line opens the csv file Start-Process $csv # the below line waits for the user to save the csv file and hit enter Read-Host "Hit Enter/Return to proceed"
Okay the start-process opens up the file but we want to wait for the user to enter the data and wait. So we will Add a Read-host command below since it pauses the script till the user enters something, in this case, just hit enter once the user is done entering the data, save the file.
$csv = Import-Csv $csv foreach ($line in $csv) { $vlan = $($line.vlan) $vlanID = $($line.vlanID) }
Processing a csv file is damn easy. the 1st line is importing the data from the $csv file and storing all that in the $variable $csv.
2nd line is going through each line of the $csv file and creating a variable. $vlan = $($line.vlan) here means that go in the line $line where the heading or column name is vlan, take the value from that cell and store it in the variable named $vlan. The same thing is true with $vlanID. Now once we have the variables for the 1st line we want to do something with that right? so let us do it.
foreach ($line in $csv) { $vlan = $($line.vlan) $vlanID = $($line.vlanID) write-host -ForeGroundColor yellow "Add Vlan $vlan $vlanID" Get-UcsLanCloud | Add-UcsVlan -DefaultNet no -Id $vlan -Name $vlanID }
So once I created the variables I am using those variables to create a vlan with a vlan ID in the last line. Above that I have another line which is just to tell the user what it is doing.
Combine all the above and now you have the script to add vlans with vlan IDs to the UCS. Yes, I leave that combining part to you. Anything where you have not put some effort of your own you won't find it interesting and struggle learn to something or do something, even if it is trivial will always make you know it than just remember it.
No comments:
Post a Comment