-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConnectionExchangeOnline.ps1
40 lines (34 loc) · 1.62 KB
/
ConnectionExchangeOnline.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
function Connect-RjRbExchangeOnline {
[CmdletBinding()]
param (
[string] $AutomationConnectionName = "AzureRunAsConnection"
)
# see RealmJoin.RunbookHelper.psm1
$Global:VerbosePreference = "SilentlyContinue"
$connectArgs = @{ ShowBanner = $false }
$connectArgsRaw = getConnectArgs 'EXO' $false $AutomationConnectionName
if ($connectArgsRaw['Identity']) {
$connectArgs += @{ ManagedIdentity = $true }
}
else {
$connectArgs += @{
Organization = $connectArgsRaw.TenantId
AppId = $connectArgsRaw.ApplicationId
CertificateThumbprint = $connectArgsRaw.CertificateThumbprint
}
}
if ($connectArgs['Organization'] -inotlike "*.onmicrosoft.com") {
Write-RjRbLog "Trying to determine initial domain name (*.onmicrosoft.com) using Graph"
Connect-RjRbGraph
$connectArgs.Organization = Invoke-RjRbRestMethodGraph /organization | Select-Object -ExpandProperty verifiedDomains | `
Where-Object { $_.isInitial } | Select-Object -First 1 -ExpandProperty name
}
Write-RjRbLog "Connecting with ExchangeOnline module" $connectArgs
if ($connectArgs['ManagedIdentity']) {
$exoVersion = (Import-Module -Name 'ExchangeOnlineManagement' -Global -PassThru).Version
if ($exoVersion -lt '3.0.0') {
throw "Connecting to Exchange Online with a Managed Identity requires at least version 3.0.0 of 'ExchangeOnlineManagement', but only version $exoVersion was found."
}
}
Connect-ExchangeOnline @connectArgs
}