SSH with Powershell: Backup multiple Cisco devices

Today, I was asked to write a script to connect to all our Cisco devices and backup the configs to our fileserver. After a few hours of figuring it out, this is what I came up with

You ONLY need a READ ONLY account on the Cisco devices.

We run this every day at 2:00 AM and backup many Cisco devices.

I downloaded and use this SSH.NET Library: SSH With Powershell

You have to move it to your powershell modules directory and then import-module

##################################
# Ed Rockwell
# Requires SSH Module from here:
# http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library
# When downloading the above, you need to right click the download and unblock it in the properties
# Then add the contents to your powershell modules
# Free to distribute! Keep: http://www.EdRockwell.com
# 5/15/2014
 
Import-Module SSH-Sessions
 
$FileServerLocation = '\\path\to\file\server\'
$DeviceList = Get-Content '\\path\to\text\file\of\devices\NetworkDeviceList.txt'
$Date = (Get-Date -f yyyyMMdd)
 
md $Date
cd $Date
 
foreach ($Device in $DeviceList)
{
New-SSHSession -ComputerName $Device -Username YourCiscoUser -Password Password4CiscoUser
$DeviceBackup = Invoke-SshCommand -ComputerName $Device -Command 'show start'
$DeviceBackup | out-file $device'.txt'
}
 
cd ..
Move-Item $Date $FileServerLocation

Leave a Reply

Your email address will not be published. Required fields are marked *