-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureAutomationRunbook-GetO365ip.ps1
446 lines (384 loc) · 13.3 KB
/
AzureAutomationRunbook-GetO365ip.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
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
<#PSScriptInfo
.VERSION 1.0
.GUID 04635c7f-df14-4e52-a166-55154ffd246d
.AUTHOR [email protected]
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
Use Microsoft Graph API to check for changes in the Office 365 Ip Addresses.
Calls to the below endoint return the version of IP address list
https://endpoints.office.com/version?clientrequestid=<client request guid>
Calls to the below endpoint return the actul list of IP address list
https://endpoints.office.com/endpoints/worldwide?clientrequestid=<client request guid>
Check version of the list. If current list version is greater than the stored version, perform the below work:
REQUIREMENTS
Office 365 service account with a mailbox.
Office 365 service account stored credentials in Azure Automation account.
Office 365 service account with role 'Contributor' on the Automation account.
.DESCRIPTION
Get the current list of O365 IP addresses
#>
#region Variables
####################################################################################################################################
####################################################################################################################################
<#
EDIT THESE VARIABLES TO MATCH YOUR CONFIGURATION
#>
####################################################################################################################################
####################################################################################################################################
$resourceGroupName = "" # Name of the Azure resource group that contains the automation account
$AutomationAccountName = "" # Name of the Azure automation account
$azureSubscriptionId = "" # Id of the Azure subscription hosting the automation account
$adminSmtpAddress = "" # smtp address of the recipient of alerts
$azureAutomaionCredentialName = "" # Name of credential stored in Azure automation for O365 service account
$notificationEmailSender = "" # this must be the O365 service account
$notificationEmailSubject = "Notification of O365 IP address change"
$smtpServer = "smtp.office365.com"
####################################################################################################################################
# Required static variables
$listVersionURL = "https://endpoints.office.com/version"
$listDataURL = "https://endpoints.office.com/endpoints/worldwide"
# regex match IPv4 addresses ignoring IPv6
$regexIPv4 = "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$"
$clientGuid = New-Guid
[string]$reqId = "?clientrequestid=$clientGuid"
$listVersionURL = "$listVersionURL" + "$reqId"
$listDataURL = "$listDataURL" + "$reqId"
#endregion
#region Get Azure Automation Credential and login
try{
$azureAutomationCredential = Get-AutomationPSCredential -Name $azureAutomaionCredentialName
Login-AzureRmAccount -Credential $azureAutomationCredential
Select-AzureRmSubscription -SubscriptionId $azureSubscriptionId
}
catch{
$_
Exit
}
Write-Output "Successfully logged into Azure with service account."
#endregion
#region Functions
Function getStoredVariables
{
<#
.DETAILS
Get stored Azure Automation variables
.STATUS
Working
.OUTPUT
Object array
#>
try {
$vars = Get-AzureRmAutomationVariable `
-ResourceGroupName $resourceGroupName `
-AutomationAccountName $AutomationAccountName `
-ErrorAction Stop
}
catch {
Throw $_
}
return $vars
}
Function getOfficeIpVersion ($URL)
{
<#
.DETAILS
Use Microsoft Graph API to get Office 365 IP addresses list version
.STATUS
Working
.OUTPUT
Int64
#>
try {
$version = Invoke-RestMethod $URL
}
catch {
Throw "Error retreiving IP list version from REST endpoint."
}
$version = $version | Where-Object instance -eq "Worldwide"
return [int]$version.Latest
}
Function getOfficeIpData ($URL, $regex)
{
<#
.DETAILS
Use Microsoft Graph API to get Office 365 IP addresses
.STATUS
Working
.OUTPUT
Object string array
#>
try {
$data = Invoke-RestMethod $URL
}
catch {
$_
Throw "Error retreiving IP list from REST endpoint."
}
$data = $data.ips
$data = $data -match $regexIPv4
return $data
}
Function notifyAdminOfChange ($adminSmtpAddress, $report)
{
$htmlhead = "<html>
<style>
BODY{font-family: Arial; font-size: 8pt;}
H1{font-size: 22px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
H2{font-size: 18px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
H3{font-size: 16px; font-family: 'Segoe UI Light','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;}
TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}
TH{border: 1px solid #969595; background: #dddddd; padding: 5px; color: #000000;}
TD{border: 1px solid #969595; padding: 5px; }
td.pass{background: #B7EB83;}
td.warn{background: #FFF275;}
td.fail{background: #FF2626; color: #ffffff;}
td.info{background: #85D4FF;}
</style>
<body>
<p>Office 365 IPs have been changed!</p>"
$htmltail = "</body></html>"
$html = $report | ConvertTo-Html | Out-String
$body = $htmlhead + $html + $htmltail
try{
Send-MailMessage `
-Credential $azureAutomationCredential `
-To $adminSmtpAddress `
-From $notificationEmailSender `
-Subject $notificationEmailSubject `
-Body $body `
-BodyAsHtml `
-UseSsl `
-SmtpServer $smtpServer
}
catch{
$_
Exit
}
}
#endregion
write-output "Successfully loaded Functions into memory."
#region Get stored variables
try {
$storedVariables = getStoredVariables
}
catch {
$_
Exit
}
#endregion
Write-Output "Successfully retreived stored variables."
#region Check for stored variables, if missing, create
If (-not($($storedVariables.Name) -like "*storedVersion*" ))
{
try {
New-AzureRmAutomationVariable `
-ResourceGroupName $resourceGroupName `
-AutomationAccountName $AutomationAccountName `
-Name "storedVersion" `
-Description "Stored version of Office 365 IP list. The version numbers can be found at https://endpoints.office.com/version." `
-Value 0 `
-Encrypted $false
$storedVariables = getStoredVariables
}
catch {
Write-Error $_
Exit
}
}
If (-not($($storedVariables.Name) -like "*storedList*" ))
{
try {
New-AzureRmAutomationVariable `
-ResourceGroupName $resourceGroupName `
-AutomationAccountName $AutomationAccountName `
-Name "storedList" `
-Description "Stored list of Office 365 IP list. The list can be found at https://endpoints.office.com/endpoints/worldwide." `
-Value "" `
-Encrypted $false
$storedVariables = getStoredVariables
}
catch {
Write-Error $_
Exit
}
}
#endregion
Write-Output "Successfully checked for stored variables."
#region Initialize stored variables
try {
$storedListVersion = ($storedVariables | Where-Object Name -eq "storedVersion").Value
}
catch {
Write-Error $_
Exit
}
try {
$storedListData = ($storedVariables | Where-Object Name -eq "storedList").Value | ConvertFrom-Json
}
catch {
Write-Error $_
Exit
}
#endregion
Write-Output "Successfully initialized stored variables"
#region Store List Data
<#
PSeudo code
If stored list is empty, seed the data and stamp the version.
If not, check the version:
if 0, set the version and seed the list
if equal, do nothing
if > 0, compare stored version to new version
if stored version -eq new version, do nothing
if stored version -lt new version
Get new ip list
compare to stored list
Item in new list but not stored? That is an update
Item in stored list but not the new? That is a delete
Set azure automation variable with new ip list
Set azure automation variable version number
Done
#>
if ($storedListData -eq "") {
# If empty, intiial seed of data
# Get list using function
# convert to json
# store in azure automation variable
try{
$ipList = getOfficeIpData $listDataURL $regexIPv4
}
catch{
$_
Exit
}
try{
$ipListJson = $ipList | ConvertTo-Json
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedList" `
-Value $ipListJson `
-Encrypted $false
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedVersion" `
-Value $ipVersion `
-Encrypted $false
}
catch{
$_
Exit
}
}
else {
# Stored list is not empty
# Get new list
# Compare
# Item in new list but not stored? That is an update
# Item in stored list but not the new? That is a delete
try{
$ipList = getOfficeIpData $listDataURL $regexIPv4
$newIpVersion = getOfficeIpVersion $listVersionURL
}
catch{
$_
Exit
}
If ($storedListVersion -eq 0){
try{
$ipListJson = $ipList | ConvertTo-Json
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedList" `
-Value $ipListJson `
-Encrypted $false
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedVersion" `
-Value $newIpVersion `
-Encrypted $false
}
catch{
$_
Exit
}
}
elseif ($storedListVersion -eq $newIpVersion){
#Nothing to do
Write-Output "List has not changed."
Exit
}
elseif ($storedListVersion -lt $newIpVersion){
#List has updated
try{
$ipList = getOfficeIpData $listDataURL $regexIPv4
$newIpVersion = getOfficeIpVersion $listVersionURL
}
catch{
$_
Exit
}
# Compare stored list to new list of IPs
try{
$compareResults = Compare-Object -ReferenceObject $storedListData -DifferenceObject $ipList
}
catch{
$_
Exit
}
# report is a pscustomobject that will be used in the email message body
$report = @()
foreach ($i in $compareResults){
If ($i.SideIndicator -eq "=>"){
$report += [PSCustomObject]@{
IP = $i.InputObject
Type = "Update"
}
}
if ($i.SideIndicator -eq "<="){
$report += [PSCustomObject]@{
IP = $i.InputObject
Type = "Delete"
}
}
}
# Store new data
try{
$ipListJson = $ipList | ConvertTo-Json
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedList" `
-Value $ipListJson `
-Encrypted $false
Set-AzureRmAutomationVariable `
-AutomationAccountName $AutomationAccountName `
-ResourceGroupName $resourceGroupName `
-Name "storedVersion" `
-Value $newIpVersion `
-Encrypted $false
}
catch{
$_
Exit
}
notifyAdminOfChange -adminSmtpAddress $adminSmtpAddress -report $report
}
else {
Write-Error "Error enumerating list version. Exiting"
Exit
}
}
#endregion
Write-output "Completed script."