This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Brocade-CopyConfigToTFTP.ps1
185 lines (151 loc) · 6.19 KB
/
Brocade-CopyConfigToTFTP.ps1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Brocade-CopyConfigToTFTP.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Script to copy startup or running config to a TFTP-Server
# Repository : https://github.com/BornToBeRoot/PowerShell_SSH-Brocade
###############################################################################################################
### Requirements ##############################################################################################
#
# - Posh-SSH (Module)
# - Brocade (Module)
# - ScanNetworkAsync.ps1 (Script)
#
###############################################################################################################
<#
.SYNOPSIS
Script to copy the running or startup config to a TFTP-Server. Useful as automatic backup using
windows task.
.DESCRIPTION
This script allowes you to scan your network (requieres ScanNetworksAsync.ps1) for all active switch devices,
identify by the Parameter -SwitchIdentifier, and copy the startup or running configuration to a TFTP-Server.
Useful as automatic backup script using windows task.
Requirements:
- Posh-SSH (Module)
- Brocade (Module)
- ScanNetworkAsync.ps1 (Script)
.EXAMPLE
.\Brocade-CopyConfigToTFTP.ps1 -TFTPServer 192.168.1.2 -StartIPAddress 192.168.2.100 -EndIPAddress 192.168.2.200
-ConfigToCopy Running
.LINK
https://github.com/BornToBeRoot/PowerShell_SSH-Brocade
https://github.com/BornToBeRoot
#>
[CmdletBinding()]
param(
[Parameter(
Position=0,
Mandatory=$true,
HelpMessage='TFTP-Server IP-Address like 192.168.0.10')]
[String]$TFTPServer,
[Parameter(
Position=1,
Mandatory=$true,
HelpMessage='Start IP-Address like 192.168.0.100')]
[String]$StartIPAddress,
[Parameter(
Position=2,
Mandatory=$true,
HelpMessage='End IP-Address like 192.168.0.200')]
[String]$EndIPAddress,
[Parameter(
Position=3,
HelpMessage='Credentials for SSH connection to Brocade switch device')]
[System.Management.Automation.PSCredential]$Credentials,
[Parameter(
Position=4,
Mandatory=$true,
HelpMessage='Copy startup or running config to tftp')]
[ValidateSet('Startup', 'Running')]
[String]$ConfigToCopy,
[Parameter(
Position=5,
HelpMessage='Switch identifier like XX_ (Only connect to devices whose hostname starts with XX_)')]
[String]$SwitchIdentifier
)
Begin{
$Script_Startup_Path = Split-Path -Parent $MyInvocation.MyCommand.Path
$ScriptFileName = $MyInvocation.MyCommand.Name
$Timestamp = Get-Date -UFormat "%Y%m%d"
# Path to the ScanNetworkAsync.ps1-script (should be placed in the same directory)
$ScanNetworkAsync_Path = "$Script_Startup_Path\ScanNetworkAsync.ps1"
# Get-Credentials for ssh session
if($Credentials -eq $null)
{
try{
$Credentials = Get-Credential $null
}
catch{
Write-Host "Entering credentials was aborted. Can't connect without credentials! Exit script..." -ForegroundColor Red
exit
}
}
# Check if ScanNetworkAsync.ps1 script exists
if(-not(Test-Path -Path $ScanNetworkAsync_Path))
{
Write-Host "Async IP-Scanner script not found! Exit script..." -ForegroundColor Red
exit
}
# Scan IP-Range with switch devices
$NetworkScan = Invoke-Expression -Command "$ScanNetworkAsync_Path -StartIPAddress $StartIPAddress -EndIPAddress $EndIPAddress"
if($NetworkScan -eq $null)
{
Write-Host "No devices found! Exit script..." -ForegroundColor Red
exit
}
# Start with actual script
$StartTime = Get-Date
$DeviceCountSuccess = 0
$DeviceCountFailed = 0
Write-Host "`n`nScript ($ScriptFileName) started at $StartTime`n" -ForegroundColor Green
Write-Host "Devices found..." -ForegroundColor Yellow
$NetworkScan # Show result of the network scan
Write-Host "`nExecuting Commands on switches..." -ForegroundColor Yellow
}
Process{
# Go through every active Brocade device
foreach($Switch in ($NetworkScan | Where-Object { $_.Status -eq "Up" -and $_.Hostname.ToLower().StartsWith($SwitchIdentifier.ToLower())}))
{
# Get the hostname
try{
$Hostname = $Switch.Hostname.Split('.')[0]
}
catch{
$Hostname = $Switch.Hostname
}
Write-Host "`nDevice:`t`t`t$Hostname" -ForegroundColor Cyan
# Create a new Brocade session
$Session = New-BrocadeSession -ComputerName $Hostname -Credentials $Credentials
# Check if session could be established
if($Session -eq $null)
{
$DeviceCountFailed ++
continue # go to next device
}
# Create command
$Command = [String]::Format("copy {0}-config tftp {1} {2}_{0}-config__{3}.bak", $ConfigToCopy.ToLower(), $TFTPServer, $Timestamp, $Hostname)
Write-Host "Command:`t`t$Command" -ForegroundColor Cyan
Write-Host "`nStart:`tHost output" -ForegroundColor Magenta
# Execute command on switch
(Invoke-BrocadeCommand -Session $Session -Command $Command -WaitTime 5000).Result
Write-Host "End:`tHost output" -ForegroundColor Magenta
# Close Brocade session
Remove-BrocadeSession -Session $Session
$DeviceCountSuccess ++
}
}
End{
# Clear credentials (should be done by garbage collector, but better safe than sorry)
$Credentials = $null
# Calculate the time between Start and End
$EndTime = Get-Date
$ExecutionTimeMinutes = (New-TimeSpan -Start $StartTime -End $EndTime).Minutes
$ExecutionTimeSeconds = (New-TimeSpan -Start $StartTime -End $EndTime).Seconds
Write-Host "`nExecuting Commands on Switches finished!" -ForegroundColor Yellow
Write-Host "`n+=-=-=-=-=-=-=-=-=-=-=-=-= Result =-=-=-=-=-=-=-=-=-=-=-=-=`n|"
Write-Host "| Successful:`t$DeviceCountSuccess"
Write-Host "| Failed:`t$DeviceCountFailed"
Write-Host "|`n+============================================================`n"
Write-Host "Script duration:`t$ExecutionTimeMinutes Minutes $ExecutionTimeSeconds Seconds`n" -ForegroundColor Yellow
Write-Host "Script ($ScriptFileName) exit at $EndTime`n" -ForegroundColor Green
}