Services/Connect-SCC.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
function Connect-SCC {

    <#
    .SYNOPSIS
    Creates a connection to Exchange online using Oauth
 
    .DESCRIPTION
    Acquires a token from Azure AD
    Uses that token to connect to Exchange Online Powershell
 
    Token lifetime is 1 hour
    The Caller must manage the session / token lifetime.
    It will pull the token from the token cache for a silent refresh of the toke if it is there.
 
    When the token expires if the session is not refreshed the Exchange shell will attempt implict remoting that will prompt / fail
 
    .LINK
    https://github.com/Canthv0/CloudConnect
 
    .OUTPUTS
    None. Creates a PS session to SCC and imports it into the global scope
 
    .EXAMPLE
    Connect-SCC
 
    Will prompt for credentials and import Security and Compliance Center PS Session into the global scope
 
    #>


    # Get the token from the service
    $Token = Get-ServiceToken -service exo

    # Check for an existing PS Session to EXO and remove it
    Get-PSSession -name scc* | Remove-PSSession -Confirm:$false

    # Build the auth information
    $Authorization = "Bearer {0}" -f $Token.Result.AccessToken
    $UserId = ($Token.Result.UserInfo.DisplayableId).tostring()
    
    # create the "basic" token to send to O365 EXO
    $Password = ConvertTo-SecureString -AsPlainText $Authorization -Force
    $Credtoken = New-Object System.Management.Automation.PSCredential -ArgumentList $UserId, $Password
    
    # Create and import the session
    $Session = New-PSSession -Name SCC -ConfigurationName Microsoft.Exchange -ConnectionUri 'https://ps.compliance.protection.outlook.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true' -Credential $Credtoken -Authentication Basic -AllowRedirection -ErrorAction Stop
    Import-Module (Import-PSSession $Session -AllowClobber) -Global -WarningAction 'SilentlyContinue'

}