My Profile Photo Title

Thoughts about DevOps and automation from a Windows guy


Backup UCS configuration with PowerShell feature image

Backup UCS configuration with PowerShell

If you’re in a situation where you need to backup your UCS configuration with PowerShell then continue reading. Below you will see how you can schedule a PowerShell script to backup all the UCS Manager configurations in your environment and save them to a SMB share.

This script relies on the following two PowerShell modules in order to function.

Cisco UCS PowerTool

You can download the UCS PowerTool module here. This is an awesome module provided by Cisco and has a TON of cmdlets. It’s safe to say that for anything you want to do in UCS, there is probably a cmdlet available.

Cisco PowerTool cmdlet count

Import / Export PS Credentials module

I’ve written about this handy module here. You use this to securely export the PS credentials needed in order to authenticate to your UCS Managers without having to expose any sensitive credentials in plain text.

These credentials can only be read by the user who exported them. This means that if you want to schedule this script via the task scheduler, then you must create and export the credentials as the user that the scheduled task will run under. I my case I use a domain service account.

Setup

  1. Log into the Windows box that will be running the scheduled task.
  2. Create your credentials that have access to UCS Manager and export said credentials to the same folder that you’ve placed the script is in.

Export PowerShell credentials to .xml file.

  1. Create a text file called “UCS Managers.txt” in the same folder. Enter the FQDN of each UCS Manager instance that your want to backup on a new line.
  2. Schedule the script via the task scheduler to under under the same service account that you are exported the credentials with.
  3. Your folder should look like this:

UCS config backup folder

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Import-Module -Name 'C:\Program Files\Cisco\Cisco UCS PowerTool\CiscoUcsPS.psd1'
Import-Module -Name Credentials

$basePath = 'C:\Scripts\Backup_UCS_Config'
$backupPath = '\\NASSERVER\Backup\UCS'
$UCSManagers = Get-Content ($basePath + '\UCS Managers.txt')
$creds = Import-PSCredential ($basePath + '\creds.xml')

#Connect to UCS
$handles = @()
$UCSManagers | ForEach-Object -Process {
  Write-Output -Object "Connecting to $_"
  $handles += Connect-Ucs -Name $_ -Credential $creds -NotDefault
}

#Backup configuration
$handles | ForEach-Object -Process {
  $ucsName = $_.Name
  Write-Output -Object ("Backing up full state for UCS $ucsName")
  Backup-Ucs -Type full-state -PathPattern ($basePath + '\${ucs}-${yyyy}${MM}${dd}-${HH}${mm}-full-state.xml') -Ucs $_
  Write-Output -Object ("Backing up logical config for UCS $ucsName")
  Backup-Ucs -Type config-logical -PathPattern ($basePath + '\${ucs}-${yyyy}${MM}${dd}-${HH}${mm}-config-logical.xml') -Ucs $_
  Write-Output -Object ("Backing up system config for UCS $ucsName")
  Backup-Ucs -Type config-system -PathPattern ($basePath + '\${ucs}-${yyyy}${MM}${dd}-${HH}${mm}-config-system.xml') -Ucs $_
  Write-Output -Object ("Backing up full config for UCS $ucsName")
  Backup-Ucs -Type config-all -PathPattern ($basePath + '\${ucs}-${yyyy}${MM}${dd}-${HH}${mm}-config-all.xml') -Ucs $_
}

#Disconnect from UCS
$handles | ForEach-Object -Process {
  Write-Output -Object "Disconnecting from $_"
  Disconnect-Ucs -Ucs $_
}

#Move backups to backup location
Write-Output -Object "Moving backups to $backuPath"
Get-ChildItem $basePath | Where-Object -FilterScript {$_.Name -match 'UC*.*xml'} | Move-Item -Destination $backupPath -Force
Sharing is caring