-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvision-Meraki.ps1
222 lines (177 loc) · 5.4 KB
/
Provision-Meraki.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
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
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER
.EXAMPLE
.LINK
https://documentation.meraki.com/zGeneral_Administration/Other_Topics/The_Cisco_Meraki_Provisioning_API
#>
param (
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[String] $Key,
[Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false)]
[String] $Organization
)
begin
{
$ApiVersion = 'v0'
$BaseUrl = 'https://dashboard.meraki.com/api'
$Headers = @{
'X-Cisco-Meraki-API-Key' = $key
'Content-Type' = 'application/json'
}
$Networks = @(
@{
'name' = 'NetLab Alderaan 1-3'
},
@{
'name' = 'NetLab Alderaan 4-6'
},
@{
'name' = 'NetLab Naboo 1-3'
},
@{
'name' = 'NetLab Naboo 4-6'
},
@{
'name' = 'NetLab Dagobah 1-3'
},
@{
'name' = 'NetLab Dagobah 4-6'
},
@{
'name' = 'NetLab Bespin 1-3'
},
@{
'name' = 'NetLab Bespin 4-6'
},
@{
'name' = 'NetLab Hoth 1-3'
},
@{
'name' = 'NetLab Hoth 4-6'
},
@{
'name' = 'NetLab Endor 1-3'
},
@{
'name' = 'NetLab Endor 4-6'
},
@{
'name' = 'NetLab Tatooine 1-3'
},
@{
'name' = 'NetLab Tatooine 4-6'
},
@{
'name' = 'NetLab Yavin 1-3'
},
@{
'name' = 'NetLab Yavin 4-6'
}
)
function Get-RedirectedUrl
{
# Get info about the organization to trigger a 302
$Response = Invoke-WebRequest -Headers $Headers -Method Get -Uri "$BaseUrl/$ApiVersion/organizations/$Organization" -MaximumRedirection 0 -ErrorAction SilentlyContinue
# If we get a 302, return the new URL. Else return the original $BaseUrl
if ($Response.StatusCode -eq 302)
{
# Capture the URL, but not the version and organization
# If the capture is successful, return it
$Url = Select-String -Pattern "^(.*)/$ApiVersion/organizations/$Organization$" -InputObject ([String] $Response.Headers.Location)
$RedirectedUrl = $Url.Matches.Groups[1].Value
if ($RedirectedUrl)
{
return $RedirectedUrl
}
else
{
return $BaseUrl
}
}
else
{
return $BaseUrl
}
}
function New-MerakiNetwork
{
param (
[Parameter(Position = 0, Mandatory = $true)]
[String] $Name,
[Parameter(Position = 1, Mandatory = $true)]
[ValidateSet('wireless', 'switch', 'appliance', 'phone')]
[String] $Type
)
$Body = @{
"name" = "$Name"
"type" = "$type"
"tags" = ''
"timeZone" = 'America/New_York'
}
$Url = Get-RedirectedUrl
$Results = Invoke-WebRequest -Headers $Headers -Method Post -Uri "$Url/$ApiVersion/organizations/$Organization/networks" -Body (ConvertTo-Json -InputObject $Body) -ErrorAction SilentlyContinue
# Check to make sure an HTTP/201 was returned
if ($Results.StatusCode -eq 201)
{
Write-Host -ForegroundColor Green "[Network] $Name successfully created"
}
else
{
Write-Host -ForegroundColor Red "[Network] ERROR: Cannot create network $Name. HTTP/$($Results.StatusCode)"
}
# Sleep for .5 seconds
Start-Sleep -Milliseconds 500
}
function Get-MerakiNetwork
{
param (
[Parameter(Position = 0, Mandatory = $false)]
[String] $Id
)
if ($Id)
{
$Results = Invoke-WebRequest -Headers $Headers -Method Get -Uri "$BaseUrl/$ApiVersion/networks/$Id"
return (ConvertFrom-Json -InputObject $Results)
}
else
{
$Results = Invoke-WebRequest -Headers $Headers -Method Get -Uri "$BaseUrl/$ApiVersion/organizations/$Organization/networks"
return (ConvertFrom-Json -InputObject $Results)
}
}
function Remove-MerakiNetwork
{
param (
[Parameter(Position = 0, Mandatory = $true)]
[String] $Id
)
# Need the real URL for this, or else we get a 308
$Url = Get-RedirectedUrl
$Results = Invoke-WebRequest -Headers $Headers -Method Delete -Uri "$Url/$ApiVersion/networks/$Id" -ErrorAction SilentlyContinue
# Check to make sure an HTTP/204 was returned
if ($Results.StatusCode -eq 204)
{
Write-Host -ForegroundColor Green "[Network] $Id successfully removed"
}
else
{
Write-Host -ForegroundColor Red "[Network] ERROR: Cannot delete network $Id. HTTP/$($Results.StatusCode)"
}
# Sleep for .5 seconds
Start-Sleep -Milliseconds 500
}
}
process
{
# Remove all existing networks
Get-MerakiNetwork | ForEach-Object {
Remove-MerakiNetwork -Id $_.Id
}
# Create the new networks
foreach ($Network in $Networks)
{
New-MerakiNetwork -Name $Network.name -Type 'switch'
}
}