profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Tuesday, November 7, 2017

Automating addition of vlans on NXOS via powershell, plink

at VCE>EMC>Dell we deliver 100s of solutions per year and all of them will have switches as usual. It is a nuisance however to create nearly 100s of vlans sometime. That too on multiple switches. If you too are someone who wanted some automation right from your windows pc then here you go.

most or all of it is at my github page. github.com/mrambig and if i ever update it then that is where you will find it.
https://github.com/MrAmbiG/cisco/blob/master/nxosAddVlan.ps1

<#
.SYNOPSIS
    add vlans to switch
.DESCRIPTION
    this will open up a csv file asking for vlan id and vlan name then it will
    configure the vlans with the following command
    config t;vlan vlanID;name vlanName
.NOTES
    File Name      : nxosAddVlan.ps1
    Author         : gajendra d ambi
    updated        : November 2017
    Prerequisite   : PowerShell v4+ over win7 and upper.
    Copyright      - None
.LINK
    Script posted over: github.com/MrAmbig/
#>

#Start of function
function GetPlink 
{
<#
.SYNOPSIS
    Gets the plink
.DESCRIPTION
    This will make sure plink is either downloaded from the internet if it is not present and if it cannot download
    then it will pause the script till you copy it manually.
.NOTES
    File Name      : GetPlink.ps1
    Author         : gajendra d ambi
    Date           : Audust 2016
    Prerequisite   : PowerShell v4+, over win7 and upper.
    Copyright      - None
.LINK
    Script posted over: 
    github.com/mrambig
    [source] http://www.virtu-al.net/2013/01/07/ssh-powershell-tricks-with-plink-exe/

#>
$PlinkLocation = $PSScriptRoot + "\Plink.exe"
$presence = Test-Path $PlinkLocation
if (-not $presence) 
    {
    Write-Host "Missing Plink.exe, trying to download...(10 seconds)" -BackgroundColor White -ForegroundColor Black
    Invoke-RestMethod "http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe" -TimeoutSec 10 -OutFile "plink.exe"
    if (-not $presence)
        {
            do
            {
            Write-Host "Unable to download plink.exe, please download and add it to the same folder as this script" -BackgroundColor Yellow -ForegroundColor Black
            Read-host "Hit Enter/Return once plink is present"
            $presence = Test-Path $PlinkLocation
            } while (-not $presence)
        }
    }

if ($presence) { Write-Host "Detected Plink.exe" -BackgroundColor White -ForegroundColor Black }
} #End of function
GetPlink

$device = Read-Host "Ip address of the device?"
$user = Read-Host "username?"
$pass = Read-Host "password?"

#Start of Script
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
$csv = "$PSScriptRoot/nxosAddVlan.csv"
get-process | Select-Object vlanID,vlanName | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation
Start-Process $csv
Read-Host "Hit Enter/Return to proceed"

echo y | C:\plink.exe -ssh $user@$device -pw $pass "exit"

$csv = Import-Csv $csv
foreach ($line in $csv) 
{
$vlanID = $($line.vlanID)  
$vlanName = $($line.vlanName)
$command = "config t ; vlan $vlanID ; name $vlanName ; end"

C:\plink.exe -ssh -v -noagent $device -l $user -pw $pass $command
}

the plink function will check for the presence of plink.exe, if not present then it will download it, copy it to c:/drive and use that to send the commands to the switch. it opens up a csv file (with excel if present). you populate the vlan id and name. save it. close it. hit enter and it starts working. it is not to save time but it will give you an unattended way of doing things. you can run it, minimize it and put your time to do something else or take a break.