forked from AlexFilipin/ConditionalAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deploy-NamedLocations.ps1
180 lines (156 loc) · 6.34 KB
/
Deploy-NamedLocations.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
function Get-GraphNamedLocation{
[cmdletbinding()]
param
(
[Parameter(Mandatory = $true)]
$accessToken,
[Parameter(Mandatory = $false)]
$All,
[Parameter(Mandatory = $false)]
$DisplayName,
[Parameter(Mandatory = $false)]
$Id
)
if($DisplayName){
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations?`$filter=DisplayName eq '$DisplayName'"
}
if($Id){
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/{$Id}"
}
if($All -eq $true){
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations"
}
$Response = Invoke-RestMethod -Method Get -Uri $URI -Headers @{"Authorization"="Bearer $accessToken"}
$Response
}
function New-GraphNamedLocation{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)]
$requestBody,
[Parameter(Mandatory = $true)]
$accessToken
)
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations"
$Response = Invoke-RestMethod -Method Post -Uri $URI -Headers @{"Authorization"="Bearer $accessToken"} -Body $requestBody -ContentType "application/json"
$Response
}
function Set-GraphNamedLocation{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)]
$requestBody,
[Parameter(Mandatory = $true)]
$accessToken,
[Parameter(Mandatory = $false)]
$Id
)
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/{$Id}"
$Response = Invoke-RestMethod -Method Patch -Uri $URI -Headers @{"Authorization"="Bearer $accessToken"} -Body $requestBody -ContentType "application/json"
$Response
}
function Remove-GraphNamedLocation{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)]
$Id,
[Parameter(Mandatory = $true)]
$accessToken
)
$URI = "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/{$Id}"
$Response = Invoke-RestMethod -Method Delete -Uri $URI -Headers @{"Authorization"="Bearer $accessToken"}
$Response
}
$Locations = Import-Csv -Path "C:\Users\filip\Downloads\NamedLocations.csv" -Delimiter ";"
foreach($Location in $Locations){
#Create body
$Body = @{
"@odata.type" = "#microsoft.graph.ipNamedLocation"
displayName = $Location.Name
isTrusted = $true
ipRanges = @($Location.cidrAddress.Foreach{
if($PSItem -like "*.*") {
@{
"@odata.type" = "#microsoft.graph.iPv4CidrRange"
cidrAddress = $PSItem
}
} else {
@{
"@odata.type" = "#microsoft.graph.iPv6CidrRange"
cidrAddress = $PSItem
}
}
})
} | ConvertTo-Json -Depth 4
#Create or patch
if($NamedLocation.value.Count -eq 1){
Write-Host "Found existing named location - patching" $Location.Name -ForegroundColor Green
Set-GraphNamedLocation -accessToken $accessToken -Id $NamedLocation.value.id -requestBody $Body
}elseif($NamedLocation.value.Count -gt 1) {
Write-Host "Found multiple existing named locations with the same name - aborting" $Location.Name -ForegroundColor Red
}else {
Write-Host "Found no existing named location - creating new one" $Location.Name -ForegroundColor Green
New-GraphNamedLocation -accessToken $accessToken -requestBody $Body
}
}
<#
SAMPLE FROM https://goodworkaround.com/2019/11/09/populating-azure-ad-named-and-trusted-locations-using-graph/
$url = Read-Host "Paste Graph Explorer url"
$excelFile = "~\Desktop\LocIP.xlsx"
# Extract access token and create header
$accessToken = ($url -split "access_token=" | select -Index 1) -split "&amp;" | select -first 1
$headers = @{"Authorization" = "Bearer $accessToken"}
# Get existing named locations
$_namedLocationsAzureAD = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/conditionalAccess/namedLocations" -Headers $headers
$namedLocationsAzureAD = $_namedLocationsAzureAD.value | foreach{[PSCustomObject]@{id = $_.id; displayName=$_.displayName; isTrusted = $_.isTrusted; ipRanges = @($_.ipranges.cidraddress)}}
# Get locations form excel
$namedLocationsExcel = @{}
Import-Excel -Path $excelFile | ? Location | Foreach {
$IP = $_.IP
if($IP -notlike "*/*" -and $IP -like "*.*") {
Write-Verbose "Changed $IP to $IP/32" -Verbose
$IP = $IP + "/32"
} elseif($IP -notlike "*/*" -and $IP -like "*:*") {
Write-Verbose "Changed $IP to $IP/128" -Verbose
$IP = $IP + "/128"
}
$namedLocationsExcel[$_.Location] += @($IP)
}
# Work in each named location in Excel
$namedLocationsExcel.Keys | Foreach {
Write-Verbose -Message "Working on location $($_) from Excel" -Verbose
$Body = @{
"@odata.type" = "#microsoft.graph.ipNamedLocation"
displayName = $_
isTrusted = $true
ipRanges = @($namedLocationsExcel[$_] | Foreach {
if($_ -like "*.*") {
@{
"@odata.type" = "#microsoft.graph.iPv4CidrRange"
cidrAddress = $_
}
} else {
@{
"@odata.type" = "#microsoft.graph.iPv6CidrRange"
cidrAddress = $_
}
}
})
} | ConvertTo-Json -Depth 4
# $Body
$existingLocation = $namedLocationsAzureAD | ? displayName -eq $_
if($existingLocation) {
$key = $_
if(($existingLocation.ipRanges | where{$_ -notin $namedLocationsExcel[$key]}) -or ($namedLocationsExcel[$key] | where{$_ -notin $existingLocation.ipRanges})) {
Write-Verbose "Location $($_) has wrong subnets -&gt; updating" -Verbose
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/conditionalAccess/namedLocations/$($existingLocation.id)" -Headers $headers -Method Patch -Body $Body -ContentType "application/json" | Out-Null
}
} else {
Write-Verbose "Location $($_) does not exist -&gt; creating" -Verbose
Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/conditionalAccess/namedLocations" -Headers $headers -Method Post -Body $Body -ContentType "application/json" | Out-Null
}
}
#>