Friday, 19 April 2019

Cisco switch configuration backup using PowerShell

Note: This blog has moved! New location vineethac.blogspot.com


In this article, I will briefly explain how to back up the running configuration of Cisco switches to a TFTP server location using PowerShell.

Prerequisites:
  • A TFTP server should be configured and running.
  • PowerShell module Posh-SSH should be installed on the node from which the script is running.

Workflow:
  1. Collect credentials to SSH into the switch
    $creds = Get-Credential
  2. Create a new SSH session to the first switch in the list
    $sw_ssh = New-SshSession -ComputerName <Management IP of Cisco switch> -Credential $creds -Force -ConnectionTimeout 300
  3. Invoke the command to backup running config to TFTP server over the SSH session
    $cmd_backup = "copy running-config tftp://<IP of TFTP server>/config_backup.txt vrf management"
    Invoke-sshcommand -Command $cmd_backup -SSHSession $sw_ssh

You can schedule this PS script using a task scheduler so that the running configuration of switches can be backed up automatically on a daily basis or as per requirements. Hope this was useful. Cheers!

Related article:
Dell EMC switch configuration backup using PowerShell

3 comments:

  1. Vini, i am looking for a script to do multiple commands like show running conf, show VLAN and saved on one file

    ReplyDelete
  2. Hi, the problem I get with above is that Cisco-ASA requires enable password. I am having problems automating authentication and getting into enable mode.

    ReplyDelete
  3. Used below
    $ssh = New-SSHSession -ComputerName $device -Credential $cred
    $DeviceBackup = Invoke-SshCommand -Command 'show start' -SSHSession $ssh
    $DeviceBackup | out-file $device'.txt'

    Got the error
    Exception calling "EndExecute" with "1" argument(s): "An established connection was aborted by the server."
    At C:\Users\v-sxt04\Documents\WindowsPowerShell\Modules\Posh-SSH\2.3.0\Posh-SSH.psm1:260 char:25
    + $Output = $_.cmd.EndExecute($_.Async)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SshConnectionException

    Can you please help

    ReplyDelete