forked from dell/OpenManage-Enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-Vlans.ps1
More file actions
256 lines (202 loc) · 7.13 KB
/
Copy pathCopy-Vlans.ps1
File metadata and controls
256 lines (202 loc) · 7.13 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#Requires -Version 7
<#
_author_ = Grant Curell <grant_curell@dell.com>
Copyright (c) 2022 Dell EMC Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
<#
.SYNOPSIS
Copies all VLANs from one OME instance to another
.DESCRIPTION
This script expects input in JSON format with two entries. The first should be a json array of dictionaries called
targets identifying the OME instances to which you want to push VLANs and the second is a single dictionary defining
the source instance. Example:
{
"target": [
{
"ip": "100.97.173.67",
"port": "443",
"user_name": "admin",
"password": "your_password"
},
{
"ip": "100.97.173.61",
"port": "443",
"user_name": "admin",
"password": "your_password"
}
],
"source": {
"ip": "100.97.173.76",
"port": "443",
"user_name": "admin",
"password": "your_password"
}
}
.PARAMETER Inputs
The name of the file containing your JSON
.EXAMPLE
.\Copy-Vlans.ps1' -inputs test.json
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Inputs
)
function Get-Data {
<#
.SYNOPSIS
Used to interact with API resources
.DESCRIPTION
This function retrieves data from a specified URL. Get requests from OME return paginated data. The code below
handles pagination. This is the equivalent in the UI of a list of results that require you to go to different
pages to get a complete listing.
.PARAMETER Url
The API url against which you would like to make a request
.PARAMETER OdataFilter
An optional parameter for providing an odata filter to run against the API endpoint.
.PARAMETER MaxPages
The maximum number of pages you would like to return
.INPUTS
None. You cannot pipe objects to Get-Data.
.OUTPUTS
dict. A dictionary containing the results of the API call or an empty dictionary in the case of a failure
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Url,
[Parameter(Mandatory = $false)]
[string]
$OdataFilter,
[Parameter(Mandatory = $false)]
[int]
$MaxPages = $null
)
$Data = @()
$NextLinkUrl = $null
try {
if ($PSBoundParameters.ContainsKey('OdataFilter')) {
$CountData = Invoke-RestMethod -Uri $Url"?`$filter=$($OdataFilter)" -Method Get -Credential $Credentials -SkipCertificateCheck
if ($CountData.'@odata.count' -lt 1) {
Write-Error "No results were found for filter $($OdataFilter)."
return @{}
}
}
else {
$CountData = Invoke-RestMethod -Uri $Url -Method Get -Credential $Credentials -ContentType $Type `
-SkipCertificateCheck
}
if ($null -ne $CountData.'value') {
$Data += $CountData.'value'
}
else {
$Data += $CountData
}
if ($CountData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($CountData.'@odata.nextLink')"
}
$i = 1
while ($NextLinkUrl) {
if ($MaxPages) {
if ($i -ge $MaxPages) {
break
}
$i = $i + 1
}
$NextLinkData = Invoke-RestMethod -Uri "$($NextLinkUrl)" -Method Get -Credential $Credentials `
-ContentType $Type -SkipCertificateCheck
if ($null -ne $NextLinkData.'value') {
$Data += $NextLinkData.'value'
}
else {
$Data += $NextLinkData
}
if ($NextLinkData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($NextLinkData.'@odata.nextLink')"
}
else {
$NextLinkUrl = $null
}
}
return $Data
}
catch [System.Net.Http.HttpRequestException] {
Write-Error "There was a problem connecting to OME or the URL supplied is invalid. Did it become unavailable?"
return @{}
}
}
function Get-Vlan {
<#
.SYNOPSIS
Grabs a list of VLANs
.DESCRIPTION
Retrieves a list of dictionaries representing the VLAN entries on a specified OME instance
.PARAMETER VlanUrl
The URL from which you want to retrieve VLANs
.OUTPUTS
List of dictionaries representing each VLAN entry in OME
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$VlanUrl
)
$VlanList = @()
$VlanData = Get-Data $VlanUrl
foreach ($Vlan in $VlanData) {
$VlanList += @{
Name = $Vlan.Name
Description = $Vlan.Description
VlanMaximum = $Vlan.VlanMaximum
VlanMinimum = $Vlan.VlanMinimum
Type = $Vlan.Type
}
}
return $VlanList
}
try {
# Read inputs
$Inputs = Get-Content $Inputs | Out-String | ConvertFrom-Json
$Password = ConvertTo-SecureString $Inputs.source.password -AsPlainText -Force
[pscredential]$Credentials = New-Object System.Management.Automation.PSCredential ($Inputs.source.user_name, $Password)
[array]$SourceVlanList = Get-Vlan "https://$($Inputs.source.ip)/api/NetworkConfigurationService/Networks"
# Loop over each target OME instance and grab its VLANs
foreach ($Target in $Inputs.target) {
$TargetUrl = "https://$($Target.ip)/api/NetworkConfigurationService/Networks"
$TargetVlanList = Get-Vlan $TargetUrl
foreach ($SourceVlanPayload in $SourceVlanList) {
Write-Host "Replicating VLAN $($SourceVlanPayload.VlanMinimum)-$($SourceVlanPayload.VlanMaximum) on target $($Target.ip)"
# Determine if VLANs overlap between source and dest OME instances
$OverlapPresent = $false
foreach ($TargetVlanPayload in $TargetVlanList) {
if ($TargetVlanPayload.VlanMinimum -le $SourceVlanPayload.VlanMaximum -and $SourceVlanPayload.VlanMinimum -le $TargetVlanPayload.VlanMaximum) {
$OverlapPresent = $true
}
}
# Push VLANs to OME
if ($OverlapPresent) {
Write-Host "WARNING: Unable to replicate VLAN $($SourceVlanPayload.VlanMinimum)-$($SourceVlanPayload.VlanMaximum) on the target $($Target.ip) as the VLANs overlap."
Write-Host $('*' * 180)
}
else {
$Response = Invoke-RestMethod -Uri $TargetUrl -Credential $Credentials -ContentType "application/json" -Method Post -Body ($SourceVlanPayload | ConvertTo-Json) -SkipCertificateCheck
write-host $Response
Write-Host $('*' * 180)
}
}
}
}
catch {
Write-Error "Exception occured at line $($_.InvocationInfo.ScriptLineNumber) - $($_.Exception.Message)"
}