functions/get-d365userauthenticationdetail.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

<#
    .SYNOPSIS
        Cmdlet used to get authentication details about a user
         
    .DESCRIPTION
        The cmdlet will take the e-mail parameter and use it to lookup all the needed details for configuring authentication against Dynamics 365 Finance & Operations
         
    .PARAMETER Email
        The e-mail address / login name of the user that the cmdlet must gather details about
         
    .EXAMPLE
        PS C:\> Get-D365UserAuthenticationDetail -Email "Claire@contoso.com"
         
        This will get all the authentication details for the user account with the email address "Claire@contoso.com"
         
    .NOTES
        Author : Rasmus Andersen (@ITRasmus)
        Author : Mötz Jensen (@splaxi)
         
#>

function Get-D365UserAuthenticationDetail {
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
        [string]$Email
    )

    $instanceProvider = Get-InstanceIdentityProvider

    [string]$identityProvider = Get-CanonicalIdentityProvider
    
    $networkDomain = get-NetworkDomain $Email

    $instanceProviderName = $instanceProvider.TrimEnd('/')
    $instanceProviderName = $instanceProviderName.Substring($instanceProviderName.LastIndexOf('/')+1)
    $instanceProviderIdentityProvider = Get-IdentityProvider "sample@$instanceProviderName"
    $emailIdentityProvider = Get-IdentityProvider $Email

    if ($instanceProviderIdentityProvider -ne $emailIdentityProvider) {
        $identityProvider = $emailIdentityProvider
    }

    $SID = Get-UserSIDFromAad $Email $identityProvider


    @{"SID"                = $SID
        "NetworkDomain"    = $networkDomain
        "IdentityProvider" = $identityProvider
        "InstanceProvider" = $instanceProvider
    }
}