-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHybridManagement (1).psm1
More file actions
330 lines (281 loc) · 10.5 KB
/
Copy pathHybridManagement (1).psm1
File metadata and controls
330 lines (281 loc) · 10.5 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
#source: //https://aka.ms/hybridconnectivity
$ErrorActionPreference = 'Stop';
Function GetAuthToken
{
param
(
[Parameter(Mandatory=$false)]
$credentials
)
Import-Module Azure
if (!$credentials) {
$credential = Get-Credential
}
else {
$credential = $credentials;
}
$userName = $credential.UserName;
$tenantName = $userName.split('@')[1];
if (!$tenantName) {
Write-Error("Unable to determine tenant from user:" + $credential.UserName);
return;
}
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://graph.microsoft.com"
$authority = "https://login.microsoftonline.com/$TenantName"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$AADCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $credential.UserName,$credential.Password
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId,$AADCredential)
return $authResult
}
Function GetAuthHeader
{
param
(
[Parameter(Mandatory=$true)]
$token)
#------Building Rest Api header with authorization token------#
$authHeader = @{
'Content-Type'='application\json'
'Authorization'=$token.CreateAuthorizationHeader()
}
return $authHeader;
}
Function Get-HybridApplication
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
$appId,
[Parameter(Mandatory=$true)]
$credential
)
#------Get the authorization token------#
$token = GetAuthToken -credential $credential;
#------Build the Rest Api header with authorization token------#
$authHeader = GetAuthHeader -Token $token;
#------Initial URI Construction------#
$uri = "https://graph.microsoft.com/edu/$tenant/applications/$appId/onPremisesPublishing"
$applications = Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Get
$applications;
return $foundApplications
}
Function Update-HybridApplication
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
$appId,
[Parameter(Mandatory=$true)]
$targetUri,
[Parameter(Mandatory=$true)]
$credential
)
#------Get the authorization token------#
$token = GetAuthToken -credential $credential;
#------Build the Rest Api header with authorization token------#
$authHeader = GetAuthHeader -Token $token;
$application = @{
"onPremisesPublishing" = @{
"applicationServerTimeout" = "Default"
"applicationType" = "microsoftapp"
"externalAuthenticationType" = "passthru"
"externalUrl" = "https://$appId.resource.mailboxmigration.his.msappproxy.net:443/"
"internalUrl" = $targetUri
"isOnPremPublishingEnabled" = $true
"isTranslateHostHeaderEnabled" = $false
"isTranslateLinksInBodyEnabled" = $false
"singleSignOnSettings" = $null
"verifiedCustomDomainCertificatesMetadata" = $null
"verifiedCustomDomainKeyCredential" = $null
"verifiedCustomDomainPasswordCredential" = $null
}
}
$uri = "https://graph.microsoft.com/edu/$tenant/applications/$appId"
$json = $application | ConvertTo-Json
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Patch -Body $json -ContentType 'application/json'
#now display results
Get-HybridApplication -appId $appId -credential $credential
}
Function New-HybridApplication
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$false)]
$appId,
[Parameter(Mandatory=$true)]
$targetUri,
[Parameter(Mandatory=$true)]
$credential
)
if (!$appId) {
$appId = New-Guid;
}
Update-HybridApplication -appId $appId -credential $credential -targetUri $targetUri;
}
Function Remove-HybridApplication
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
$appId,
[Parameter(Mandatory=$true)]
$credential
)
#------Get the authorization token------#
$token = GetAuthToken -credential $credential;
#------Build the Rest Api header with authorization token------#
$authHeader = GetAuthHeader -Token $token;
$application = @{
"onPremisesPublishing" = $null
}
$json = $application | ConvertTo-Json
$uri = "https://graph.microsoft.com/edu/$tenant/applications/$appId"
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Patch -Body $json -ContentType 'application/json'
}
Function Get-HybridAgent
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
$credential
)
#------Get the authorization token------#
$token = GetAuthToken -credential $credential;
#------Build the Rest Api header with authorization token------#
$authHeader = GetAuthHeader -Token $token;
#------Initial URI Construction------#
$uri = "https://graph.microsoft.com/edu/connectorGroups?`$expand=members"
$result = Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Get
return $result.value.members;
}
Function TestoneEndpoint
{
param
(
[Parameter(Mandatory=$true)]
$endpoint,
$testGet)
Write-Host("Testing connection to " + $endpoint.uri + " on port " + $endpoint.port)
test-netconnection $endpoint.uri -Port $endpoint.port
if ($testGet) {
$uri = 'https://' + $endpoint.uri + ':' + $endpoint.port;
Write-Host("Performing GET on " + $uri)
$result = Invoke-WebRequest -Method Get -Uri $uri
if ($result.StatusCode -ne 200)
{
Write-Host("Failed to get content with status " + $result.StatusCode);
}
}
}
Function TestEndpoints
{
param
(
[Parameter(Mandatory=$true)]
$endpoints,
$testGet)
foreach ($endpoint in $endpoints) {
$result = TestoneEndpoint $endpoint $testGet
if ($result.TcpTestSucceeded -ne $true) {
Write-Host("Failed to connecto to " + $endpoint.uri + ":" + $endpoint.port);
}
}
}
Function TestProxySettings
{
$proxy = [System.Net.WebProxy]::GetDefaultProxy() | select address
if ($proxy.Address -ne $null)
{
Write-Host("WebRequest is configured for proxy:" + $proxy.Address);
Write-Host("Ensure connector is configured for proxy or whitelisted to bypass proxy.");
}
$browserProxies = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer
if ($browserProxies -ne $null)
{
Write-Host("Browser is configured for proxy:" + $browserProxies.Address);
Write-Host("Ensure connector is configured for proxy or whitelisted to bypass proxy.");
}
}
Function TestTLSSettings
{
$regKey = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2"
$tlsKeyExists = Test-Path $regKey
if ($tlsKeyExists -eq $false) {
Write-Warning("Registry Key does not exist:" + $regKey);
Write-Warning("TLS 1.2 is not explicitly enabled. Please enable it.");
return;
}
$regKey = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client";
$tlsClientKeyExists = Test-Path $regKey
if ($tlsClientKeyExists -eq $false) {
Write-Warning("Registry Key does not exist:" + $regKey);
Write-Warning("TLS 1.2 is not explicitly enabled. Please enable it.");
return;
}
$clientEnabled = (Get-ItemProperty -Path $regKey).Enabled
$clientDisabledByDefault = (Get-ItemProperty -Path $regKey).DisabledByDefault
if (($clientDisabledByDefault) -eq $true -or ($clientEnabled -eq $false)) {
Write-Warning("TLS 1.2 is not explicitly enabled. Please enable it.");
return;
}
$regKey = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server";
$tlsServerKeyExists = Test-Path $regKey
if ($tlsServerKeyExists -eq $false) {
Write-Warning("TLS is either not enabled or disabled by default:" + $regKey);
Write-Warning("TLS 1.2 is not explicitly enabled. Please enable it.");
return;
}
$serverEnabled = (Get-ItemProperty -Path $regKey).Enabled
$serverDisabledByDefault = (Get-ItemProperty -Path $regKey).DisabledByDefault
if (($serverDisabledByDefault) -eq $true -or ($serverEnabled -eq $false)) {
Write-Warning("TLS is either not enabled or disabled by default:" + $regKey);
Write-Warning("TLS 1.2 is not explicitly enabled. Please enable it.");
return;
}
}
Function Test-HybridConnectivity
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$false)]
[Switch]
$testO365Endpoints)
$httpEndpoints = @(
[pscustomobject]@{uri = 'mscrl.microsoft.com'; port = '80' },
[pscustomobject]@{uri = 'crl.microsoft.com'; port = '80' },
[pscustomobject]@{uri = 'ocsp.msocsp.com'; port = '80' },
[pscustomobject]@{uri = 'www.microsoft.com'; port= '80' })
$httpsEndpoints = @(
[pscustomobject]@{uri = 'login.windows.net'; port = '443' },
[pscustomobject]@{uri = 'login.microsoftonline.com'; port= '443' })
<# # TODO - Derive this automatically from the bootstrap endpoint.
$appProxyEndpoints = @(
[pscustomobject]@{uri = 'watchdog.servicebus.windows.net'; port= '443' }) #>
$o365Endpoints = @(
[pscustomobject]@{uri = 'outlook.office.com'; port = '443' },
[pscustomobject]@{uri = 'outlook.office365.com'; port = '443' },
[pscustomobject]@{uri = 'nexus.microsoftonline-p.com'; port = '443' },
[pscustomobject]@{uri = 'login.microsoftonline.com'; port= '443' })
# CRL Endpoints
TestEndpoints $httpEndpoints
# Logon Endpoints
TestEndpoints $httpsEndpoints
<# # AppProxy Endpoints
TestEndpoints $appProxyEndpoints $true #>
if ($testO365Endpoints) {
# O365 Endpoints
TestEndpoints $o365Endpoints $true
}
# Test TLS Configuration
TestTLSSettings
# Test Proxy Settings
TestProxySettings
}