In the first post of this series, we learned how to automatically deploy switch firmware through PowerShell. Don’t get me wrong, this is awesome! But if you don’t have your switch configuration saved already, you would have to be bloody insane to push a new firmware to the device. No offense to those insane readers who did this. 🙂
So what is an admin to do? Why, use PowerShell to connect to each switch and save the configuration files directly!
How to Backup Switch Configurations with PowerShell
$Server = "192.168.0.10" $Username = "administrator" $Password = "password" $Date = (Get-Date -UFormat "%Y-%m-%d") Set-Location C:\putty\ $SwitchList = Import-Csv .\switchlist.csv | sort NodeName foreach ($Switch in $SwitchList){ $FilePath = "Configs\" + $Switch.NodeName + "\" $FileName = $Switch.IPAddress + "_" + $Date +".txt" if ((Test-Path \\$Server\c$\TFTP-Root\$Filepath) -eq $false){New-Item -ItemType Directory -Name $Switch.NodeName -Path \\$Server\c$\TFTP-Root\Configs\} .\kitty.exe $Switch.IPAddress -ssh -v -l $Username -pw $Password -cmd "\s05 \n Copy Startup-Config tftp $Server $Filepath$FileName \n y \n logout \n y \n y \n y" sleep -Seconds 2 }
I won’t bore you with the setup steps required for this script – read the previous article in this series to generate your switchlist file and to configure your TFTP server. If you’ve already done those steps, this script should run without any issues. Just change the configuration block at the top and edit the switch command line to match your environment. Ensure that your TFTP server has a subfolder named Configs in it.
.\kitty.exe $Switch.IP_Address -ssh -v -l $Username -pw $Password -cmd "\s05 \n Copy Startup-Config tftp $Server $Filepath$FileName \n y \n logout \n y \n y \n y"
This script is already configured to backup the configuration for an HP ProCurve Switch. If you have other makes in your environment, leave a comment below with your script alterations.
Startup Configurations will be saved according to the $FilePath and $FileName variables. By default, the script will create a folder under the Configs subfolder for each switch on your TFTP server. The folder will be named after the Switch Host/Node name. This information is pulled from your Switchlist.csv. Each saved configuration is named after the switch’s IP address with a date attached to the end of the file (ex: 10.95.0.1_2016-02-29.txt).
What are you waiting for? Start backing up all of your switch configurations. Schedule this script for automated backups! As with any backup, verify the data. In our next post of this series, we will use PowerShell to change our switch configurations.