-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerFMC.psm1
361 lines (328 loc) · 13.6 KB
/
PowerFMC.psm1
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#############################################################################################################
#############################################################################################################
#############################################################################################################
function Get-FMCAuthToken {
<#
.SYNOPSIS
Obtains Domain UUID and X-auth-access-token
.DESCRIPTION
This cmdlet will invoke a REST post against the FMC API, authenticate, and provide an X-auth-access-token and
Domain UUID for use in other functions
.EXAMPLE
# Get-FMCAuthToken -fmcHost 'https://fmcrestapisandbox.cisco.com' -username 'davdecke' -password 'YDgQ7CBR'
.PARAMETER fmcHost
Base URL of FMC
.PARAMETER username
REST account username
.PARAMETER password
REST account password
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$fmcHost,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$username,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$password
)
Begin {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
Process {
$credPair = "$($username):$($password)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
$uri = "$fmcHost/api/fmc_platform/v1/auth/generatetoken"
$headers = @{ Authorization = "Basic $encodedCredentials" }
$AuthResponse = Invoke-WebRequest -Uri $uri -Headers $headers -Method Post
$Domain = $AuthResponse.Headers.Item('DOMAIN_UUID')
$AuthAccessToken = $AuthResponse.Headers.Item('X-auth-access-token')
}
End {
$output = New-Object -TypeName psobject
$output | Add-Member -MemberType NoteProperty -Name fmcHost -Value $fmcHost
$output | Add-Member -MemberType NoteProperty -Name Domain -Value $Domain
$output | Add-Member -MemberType NoteProperty -Name AuthAccessToken -Value $AuthAccessToken
$output
}
}
#############################################################################################################
#############################################################################################################
#############################################################################################################
function Get-FMCNetworkObjects {
<#
.SYNOPSIS
Displays network objects in FMC
.DESCRIPTION
This cmdlet will invoke a REST request against the FMC API and retrieve items under /object/networks
.EXAMPLE
# Get-FMCNetworkObjects -fmcHost "https://fmcrestapisandbox.cisco.com" -username 'davdecke' -password 'xxxxxx'
.PARAMETER fmcHost
Base URL of FMC
.PARAMETER AuthAccessToken
X-auth-accesss-token
.PARAMETER Domain
Domain UUID
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$fmcHost,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$AuthAccessToken,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$Domain
)
Begin {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
Process {
$uri = "$fmcHost/api/fmc_config/v1/domain/$Domain/object/networks"
$headers = @{ "X-auth-access-token" = "$AuthAccessToken" }
$response = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
$NetObjects = @()
$response.items.links.self | foreach {
$NetObjects += Invoke-RestMethod -Method Get -Uri $_ -Headers $headers
}
}
End {
$NetObjects
}
}
#############################################################################################################
###################5##########################################################################################
#############################################################################################################
function New-FMCNetworkObject {
<#
.SYNOPSIS
Create network objects in FMC
.DESCRIPTION
This cmdlet will invoke a REST request against the FMC API and retrieve items under /object/networks
.EXAMPLE
# $fmcHost = 'https://fmcrestapisandbox.cisco.com'
# $a = Get-FMCAuthToken -fmcHost $fmcHost -username 'davdecke' -password 'xxxxxx'
# $a | New-FMCNetworkObject -fmcHost $fmcHost -name 'PowerFMC_172.21.33.0/24' -Network "172.21.33.0" -Prefix 24 -description "Test Object for PowerFMC 2"
.PARAMETER fmcHost
Base URL of FMC
.PARAMETER AuthAccessToken
X-auth-accesss-token
.PARAMETER Domain
Domain UUID
.PARAMETER name
Name of the rule. Illegal characters (/,\,whitespaces) are automatically replaced with underscrores
.PARAMETER Network
The network or host dotted-decimal IP
.PARAMETER Prefix
Prefix length for network (32 for host)
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$fmcHost,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$Domain,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$AuthAccessToken,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$name,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$description,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$overridable="false",
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[ValidateSet("network","host","range")]
[string]$type="network",
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$Network
)
Begin {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
Process {
$uri = "$fmcHost/api/fmc_config/v1/domain/$Domain/object/networks"
$headers = @{ "X-auth-access-token" = "$AuthAccessToken" ;'Content-Type' = 'application/json' }
$name = $name -replace '(\\|\/|\s)','_'
$body = New-Object -TypeName psobject
$body | Add-Member -MemberType NoteProperty -name name -Value $name
$body | Add-Member -MemberType NoteProperty -name value -Value "$Network"
$body | Add-Member -MemberType NoteProperty -name overridable -Value $overridable
$body | Add-Member -MemberType NoteProperty -name description -Value "$description"
$body | Add-Member -MemberType NoteProperty -name type -Value $type
$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body ($body | ConvertTo-Json)
$response
}
End {}
}
#############################################################################################################
#############################################################################################################
#############################################################################################################
function Get-FMCNetworkGroups {
<#
.SYNOPSIS
Displays network groups in FMC
.DESCRIPTION
This cmdlet will invoke a REST request against the FMC API and retrieve items under /object/networkgroups
.EXAMPLE
# Get-FMCNetworkObjects -fmcHost "https://fmcrestapisandbox.cisco.com" -AuthAccessToken 'e276abec-e0f2-11e3-8169-6d9ed49b625f' -Domain '618846ea-6e3e-4d69-8f30-55f31b52ca3e'
.PARAMETER fmcHost
Base URL of FMC
.PARAMETER AuthAccessToken
X-auth-accesss-token
.PARAMETER Domain
Domain UUID
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$fmcHost,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$AuthAccessToken,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$Domain
)
Begin {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
Process {
$uri = "$fmcHost/api/fmc_config/v1/domain/$Domain/object/networkgroups"
$headers = @{ "X-auth-access-token" = "$AuthAccessToken" }
$response = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
$NetObjects = @()
$response.items.links.self | foreach {
$NetObjects += Invoke-RestMethod -Method Get -Uri $_ -Headers $headers
}
$NetObjects
}
End {}
}
#############################################################################################################
#############################################################################################################
#############################################################################################################
function New-FMCNetworkGroup {
<#
.SYNOPSIS
Create network groups in FMC
.DESCRIPTION
This cmdlet will invoke a REST request against the FMC API and create Network Groups
.EXAMPLE
# $fmcHost = 'https://fmcrestapisandbox.cisco.com'
# $a = Get-FMCAuthToken -fmcHost $fmcHost -username 'davdecke' -password 'xxxxxx'
# $a | New-FMCNetworkGroup -fmcHost $fmcHost -name 'PowerFMC_TestGroup' -members 'PowerFMC_TestObj1,PowerFMC_TestObj2,PowerFMC_TestObj3' -description "Group for PowerFMC"
.PARAMETER fmcHost
Base URL of FMC
.PARAMETER AuthAccessToken
X-auth-accesss-token
.PARAMETER Domain
Domain UUID
.PARAMETER name
Name of the rule. Illegal characters (/,\,whitespaces) are automatically replaced with underscrores
.PARAMETER Network
The network or host dotted-decimal IP
.PARAMETER Prefix
Prefix length for network (32 for host)
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$name,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$members,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$description,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$overridable="false",
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$fmcHost,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$Domain,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$AuthAccessToken
)
Begin {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
Process {
$uri = "$fmcHost/api/fmc_config/v1/domain/$Domain/object/networkgroups"
$headers = @{ "X-auth-access-token" = "$AuthAccessToken" ;'Content-Type' = 'application/json' }
$name = $name -replace '(\\|\/|\s)','_'
$members = $members -split ','
$literals = @()
$members | foreach {
$literal = New-Object -TypeName psobject
$literal | Add-Member -MemberType NoteProperty -Name value -Value $_
$literals += $literal
}
$body = New-Object -TypeName psobject
$body | Add-Member -MemberType NoteProperty -name type -Value "NetworkGroup"
$body | Add-Member -MemberType NoteProperty -name literals -Value $literals
$body | Add-Member -MemberType NoteProperty -name overridable -Value $overridable
$body | Add-Member -MemberType NoteProperty -name description -Value "$description"
$body | Add-Member -MemberType NoteProperty -name name -Value "$name"
$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body ($body | ConvertTo-Json)
$response
}
End {}
}
#############################################################################################################
#############################################################################################################
#############################################################################################################