-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeraki-API-Functions.ps1
More file actions
468 lines (370 loc) · 12.6 KB
/
Copy pathMeraki-API-Functions.ps1
File metadata and controls
468 lines (370 loc) · 12.6 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<#
.NOTES
===========================================================================
Created by: James Krolik
Created on: 08/01/2022
Updated on: 08/01/2022
Filename: Meraki-API-Functions.ps1
===========================================================================
.DESCRIPTION
This script is a dump of functions I've built for managing Meraki clients to ensure consistency.
Originally, this was built to ensure that all of our clients had the API enabled, SAML enabled,
the certificate thumbprint installed for SAML, and the SAML role created.
It has since evolved to include other items, so I felt this would be a good starting point for anyone
wanting to learn about interacting with Meraki's v1 API with PowserShell. If new functions are needed,
they should be able to be adapted from Meraki's API documentation.
Configurations that should be done prior to executing:
Add your API key
Add your certificate thumbprint if using SAML
The functional workflow of this script is as follows:
Get all organizations
Check if API management is enabled and enable if it is not.
Check if SAML is enabled and enable it if it is not.
Check if a certificate thumbprint exists and compare with the newCertificateThumbprint.
Update the certificate if they do not match.
Check if our account is in the SAML group and add it if it is missing.
Finally, loop through and add our new admin account to all clients if it is not present.
#>
cls
#The API key will be for the main administrator account.
$apiKey = "" # <== Enter your API key here
#This will be the thumbprint we want to standardize on.
$newCertificateThumbprint = "" # <== Enter your certificate thumbprint here in the format AA:BB:CC...
#If you are using SAML change this to whatever you want the acocunt to be called.
$samlGroupName = "_yourSAMLname_"
#If you are going to add a new admin to every group, change the parameters here.
$newAdminName = ""
$newAdminEmail = ""
$headers = @{
"X-Cisco-Meraki-API-Key"=$apiKey
"Content-Type" = "application/json"
}
<#################
## API Functions #
##################>
function isAPIEnabled() {
<#
This function is used to query the organization to see if API is enabled or not.
When called, it returns the boolean of whether or not it is enabled.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id"
"Method" = 'GET'
}
$apicheck = Invoke-RestMethod @Params -headers $headers
return $apicheck.api.enabled
}
function enableAPI() {
<#
This function is called when we want to enable the API on a client that is not currently set on.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id"
"Method" = 'PUT'
}
#Construct our body for sending the updated settings.
$body = @{
"api" = @{ "enabled" = $true };
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
$apicheck = Invoke-RestMethod @Params -headers $headers -body $jsonbody
}
<##################
## SAML Functions #
###################>
function isSAMLEnabled() {
<#
This function is used to query the organization to see if SAML is enabled or not.
When called, it returns the boolean of whether or not it is enabled.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml"
"Method" = 'GET'
}
$samlCheck = Invoke-RestMethod @Params -headers $headers
return $samlCheck.enabled
}
function enableSAML() {
<#
This function is used to enable the SAML when called.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml"
"Method" = 'PUT'
}
#Construct our body for sending the updated settings.
$body = @{
"enabled" = $true
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
Invoke-RestMethod @Params -headers $headers -body $jsonbody
}
function addToSAML() {
<#
This function is used to add a SAML role and grant 'full' tenant access.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/samlRoles"
"Method" = 'POST'
}
#Construct our body for sending the updated settings.
$body = @{
"role" = $samlGroupName
"orgAccess" = "full"
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
Invoke-RestMethod @Params -headers $headers -body $jsonbody
write-host "Added $samlGroupName to SAML database."
}
function checkIfGroupIsInSAML() {
<#
This function is used to check if $samlGroupName is in the SAML administrators group.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/samlRoles"
"Method" = 'GET'
}
$isInSAML = Invoke-RestMethod @Params -headers $headers
if ($isInSAML.role -contains $samlGroupName) {
return $true
}
else {
return $falase
}
}
<#################
## IDP Functions #
##################>
function getIdpId() {
<#
This function will return the SAML idpID required for updating the certificate fingerprint.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml/idps"
"Method" = 'GET'
}
$idps = Invoke-RestMethod @Params -headers $headers
return $idps.idpId
}
function createIdpId() {
<#
This function will create the idpId and inject the certificate fingerprint at the same time.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id,
[Parameter(Mandatory=$true)]
[String]$cert
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml/idps"
"Method" = 'POST'
}
#Construct our body for sending the updated settings.
$body = @{
"x509certSha1Fingerprint" = $cert
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
Invoke-RestMethod @Params -headers $headers -body $jsonbody
}
function addCertificateThumbprint() {
<#
This function will update the Certificate Thumbprint
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id,
[Parameter(Mandatory=$true)]
[String]$cert,
[Parameter(Mandatory=$true)]
[String]$idpId
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml/idps/$idpId"
"Method" = 'PUT'
}
#Construct our body for sending the updated settings.
$body = @{
"x509certSha1Fingerprint" = $cert
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
Invoke-RestMethod @Params -headers $headers -body $jsonbody
}
function getCertificateThumbprint() {
<#
This function will return the current Certificate Thumbprint
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/saml/idps"
"Method" = 'GET'
}
$certificate = Invoke-RestMethod @Params -headers $headers
return $certificate.x509certSha1Fingerprint
}
<###################
# Admin Management #
###################>
function getOrganizationAdmins() {
<#
This function will return all of the organization admins for an organization.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/admins"
"Method" = 'GET'
}
$orgAdmins = Invoke-RestMethod @Params -headers $headers
return $orgAdmins
}
function addOrganizationAdmins() {
<#
This function will return all of the organization admins for an organization.
#>
Param (
[Parameter(Mandatory=$true)]
[String]$id
)
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations/$id/admins"
"Method" = 'POST'
}
#Construct our body for sending the updated settings.
$body = @{
"name" = $newAdminName
"email" = $newAdminEmail
"orgAccess" = "full"
};
#Convert to JSON as needed and invoke the request.
$jsonbody = $body | ConvertTo-Json
Invoke-RestMethod @Params -headers $headers -body $jsonbody
write-host "Added $newAdminEmail to organization administrators."
}
<################
# Program Begin #
################>
#Get list of organizations
$Params = @{
"URI" = "https://api.meraki.com/api/v1/organizations"
"Method" = 'GET'
}
$listOfOrgs = Invoke-RestMethod @Params -headers $headers
#Loop through each organization.
$listOfOrgs.ForEach({
write-host "Org ID: " $_.id
write-host "Org Name: " $_.name
#Check if API is enabled.
$isAPIEnabled = isAPIEnabled -id $_.id
write-host "API: " $isAPIEnabled
if ($isAPIEnabled -eq $true) {
#Check if SAML is enabled
$isSAMLEnabled = isSAMLEnabled -id $_.id
write-host "SAML: " $isSAMLEnabled
if ($isSAMLEnabled -eq $true) {
#Get the certificate thumbprint and output if present.
$certificateThumbprint = getCertificateThumbprint -id $_.id
write-host "Certificate Thumbprint: " $certificateThumbprint
write-host ""
}
else {
#Verbose output if SAML isn't enabled and then call the function to enable it.
write-warning "Please enable SAML to query certificate thumbprint."
write-host "Attempting to enable SAML."
write-host ""
enableSAML -id $_.id
}
#Get the certificate thumbprint
$certificateThumbprint = getCertificateThumbprint -id $_.id
#If the thumbprint is not set, alert and do a further check to attempt to correct it.
if ($certificateThumbprint -eq $null) {
write-warning "No certificate thumbprint found"
write-host "Attempting to update thumbprint."
#In order to see if the thumbprint is just not set or if the whole section is disabled, check for the presence of
#an IDP attribute
$idpId = getIdpId -id $_.id
$id = $_.id
try {
#Attempt to add and if it fails, then the IDP is not set, so catch the error and create it.
addCertificateThumbprint -id $id -cert $newCertificateThumbprint -idpID $idpId
}
catch {
createIdpId -id $id -cert $newCertificateThumbprint
}
} #end certificateThumbprint $null check.
} #end isAPIEnabled Check
else { #API is currently not enabled. Verbose output and attempt to enable it.
write-warning "Please enable API to query SAML."
write-host "Attempting to enable API."
enableAPI -id $_.id
write-warning "API needs to be enabled in order to query certificate thumbprint."
write-host ""
}
#Now that we've made it this far, check if your SAML group is NOT present in the roles for the SAML database, and if not, add it.
if (!(checkIfGroupIsInSAML -id $_.id)) {
write-warning "$samlGroupName is not in the SAML database for this client."
addToSAML -id $_.id
write-host ""
}
#Add new admin account as an organizational admin.
$orgAdmins = getOrganizationAdmins -id $_.id
write-host "List of Admins:"
$accountToAddFound = $false
try { #Enumerate each administrator in the list. This will throw an error if only one exists, hence the try statement.
$orgAdmins.ForEach({
$email = $_.email
if ($email -eq $newAdminEmail) {
$accountToAddFound = $true
}
})
}
catch { #If we catch, there is only one administrator, so perform the check for adding.
if ($orgAdmins.email -eq $newAdminEmail) {
$accountToAddFound = $true
}
}
write-host " "
if ($accountToAddFound -eq $false) {
write-host "Adding new admin account Account"
addOrganizationAdmins -id $_.id
$accountToAddFound = $false
}
})