Commands/Connect-MSGraphAPI.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-MSGraphAPI {
    [CmdletBinding(DefaultParameterSetName='GetGraphClientByUserAndPassword')]
    Param(
        [Parameter(Mandatory=$true, ParameterSetName='GetGraphClientByUserAndPassword')]
            [string]$Tenant,
        [Parameter(Mandatory=$true, ParameterSetName='GetGraphClientByUserAndPassword')]
            [string]$ClientId,
        [Parameter(Mandatory=$true, ParameterSetName='GetGraphClientByUserAndPassword')]
            [string]$RedirectURI,
        [Parameter(Mandatory=$true, ParameterSetName='GetGraphClientByUserAndPassword')]
            [string]$Resource,
        [Parameter(Mandatory=$true, ParameterSetName='GetGraphClientByUserAndPassword')]
            [pscredential]$Credential
    )
    if($PSCmdlet.ParameterSetName -eq 'GetGraphClientByUserAndPassword'){
        $token = [MSGraphAPI.ConnectADAL]::GetGraphClientByUserAndPassword(
            $Tenant,
            $ClientId,
            $RedirectURI,
            $Resource,
            $Credential.UserName,
            $Credential.GetNetworkCredential().SecurePassword
        )
        $Header = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
        $null = $Header.Add("Authorization", "Bearer $($token.AccessToken)")
        $Script:MSGraphAPISettings = New-Object -TypeName MSGraphAPISettings -Property @{
            AuthorizationHeader = $Header
            ConnectParameters = @{
                Tenant = $Tenant
                ClientId = $ClientId
                RedirectURI = $RedirectURI
                Resource = $Resource
                Credential = $Credential
            }
            ExpiresOn = $Token.ExpiresOn.UtcDateTime
        }
    }
}