functions/import-d365aaduser.ps1

<#
.SYNOPSIS
Used to import Aad users into D365FO
 
.DESCRIPTION
Provides a method for importing a AAD UserGroup or a comma separated list of AadUsers into D365FO.
 
.PARAMETER AadGroupName
Azure Active directory user group containing users to be imported
 
.PARAMETER UserList
A comma separated list of Aad users to be imported into D365FO
 
.PARAMETER StartupCompany
Startup company of users imported.
 
Default is DAT
 
.PARAMETER DatabaseServer
Alternative SQL Database server, Default is the one provided by the DataAccess object
 
.PARAMETER DatabaseName
Alternative SQL Database, Default is the one provided by the DataAccess object
 
.PARAMETER SqlUser
Alternative SQL user, Default is the one provided by the DataAccess object
 
.PARAMETER SqlPwd
Alternative SQL user password, Default is the one provided by the DataAccess object
 
.PARAMETER IdPrefix
A text that will be prefixed into the ID field. E.g. -IdPrefix "EXT-" will import users and set ID starting with "EXT-..."
 
.PARAMETER NameSuffix
A text that will be suffixed into the NAME field. E.g. -NameSuffix "(Contoso)" will import users and append "(Contoso)"" to the NAME
 
.PARAMETER IdValue
Specify which field to use as ID value when importing the users.
Available options 'Login' / 'FirstName'
 
Default is 'Login'
 
.PARAMETER NameValue
Specify which field to use as NAME value when importing the users.
Available options 'FirstName' / 'DisplayName'
 
Default is 'DisplayName'
 
.PARAMETER AzureAdCredential
Use a PSCredential object for connecting with AzureAd
 
.EXAMPLE
Import-D365AadUser -Users "Claire@contoso.com","Allen@contoso.com"
 
Imports Claire and Allen as users.
 
 
.EXAMPLE
$myPassword = ConvertTo-SecureString "MyPasswordIsSecret" -AsPlainText -Force
$myCredentials = New-Object System.Management.Automation.PSCredential ("MyEmailIsAlso", $myPassword)
 
Import-D365AadUser -Users "Claire@contoso.com","Allen@contoso.com" -AzureAdCredential $myCredentials
 
Imports Claire and Allen as users.
 
 
.EXAMPLE
Import-D365AadUser -AadGroupName "CustomerTeam1"
 
Imports all the users that is present in the AAD Group called CustomerTeam1
 
.NOTES
At no circumstances can this cmdlet be used to import users into a PROD environment.
 
Only users from an Azure Active Directory that you have access to, can be imported.
Use AAD B2B implementation if you want to support external people.
 
Every imported users will get the System Administration / Administrator role assigned on import
#>


function Import-D365AadUser {
    [CmdletBinding(DefaultParameterSetName = 'UserListImport')]
    param (
        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "GroupImport")]
        [String]$AadGroupName,

        [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "UserListImport")]
        [string[]]$Users,

        [Parameter(Mandatory = $false, Position = 2)]
        [string]$StartupCompany = 'DAT',

        [Parameter(Mandatory = $false, Position = 3)]
        [string]$DatabaseServer = $Script:DatabaseServer,

        [Parameter(Mandatory = $false, Position = 4)]
        [string]$DatabaseName = $Script:DatabaseName,

        [Parameter(Mandatory = $false, Position = 5)]
        [string]$SqlUser = $Script:DatabaseUserName,

        [Parameter(Mandatory = $false, Position = 6)]
        [string]$SqlPwd = $Script:DatabaseUserPassword,

        [Parameter(Mandatory = $false, Position = 7)]
        [string]$IdPrefix = "",

        [Parameter(Mandatory = $false, Position = 8)]
        [string]$NameSuffix = "",

        [Parameter(Mandatory = $false, Position = 9)]
        [ValidateSet('Login', 'FirstName')]
        [string]$IdValue = "Login",

        [Parameter(Mandatory = $false, Position = 10)]
        [ValidateSet('FirstName', 'DisplayName')]
        [string]$NameValue = "DisplayName",

        [Parameter(Mandatory = $false, Position = 11)]
        [PSCredential]$AzureAdCredential,
        [Parameter(Mandatory = $false, Position = 12, ParameterSetName = "UserListImport")]
        [switch]$SkipAzureAd
    )

    $UseTrustedConnection = Test-TrustedConnection $PSBoundParameters

    $SqlParams = @{ DatabaseServer = $DatabaseServer; DatabaseName = $DatabaseName;
        SqlUser = $SqlUser; SqlPwd = $SqlPwd 
    }


    $SqlCommand = Get-SqlCommand @SqlParams -TrustedConnection $UseTrustedConnection

    $instanceProvider = Get-InstanceIdentityProvider
    $canonicalProvider = Get-CanonicalIdentityProvider

    try {
        Write-PSFMessage -Level Verbose -Message "Trying to connect to the Azure Active Directory"

        if ($PSBoundParameters.ContainsKey("AzureAdCredential") -eq $true) {
            $null = Connect-AzureAD  -ErrorAction Stop -Credential $AzureAdCredential
        }
        else {
            if($SkipAzureAd -eq $false) {
                $null = Connect-AzureAD  -ErrorAction Stop
            }
        }


    }
    catch {
        Write-PSFMessage -Level Host -Message "Something went wrong while connecting to Azure Active Directory" -Exception $PSItem.Exception
        Stop-PSFFunction -Message "Stopping because of errors"
        return
    }
    

    $azureAdUsers = New-Object -TypeName "System.Collections.ArrayList"

    if ( $PSCmdlet.ParameterSetName -eq "GroupImport") {

        $group = Get-AzureADGroup -SearchString $AadGroupName

        if ($null -eq $group) {
            Write-PSFMessage -Level Host -Message "Unable to find the specified group in the AAD. Please ensure the group exists and that you have enough permissions to access it."
            Stop-PSFFunction -Message "Stopping because of errors"
            return
        }

        if ($group.Length -gt 1) {
            Write-PSFMessage -Level Host -Message "More than one group found"
            foreach ($foundGroup in $group) {
                Write-PSFMessage -Level Host -Message "Group found $($foundGroup.DisplayName)"
            }
            Stop-PSFFunction -Message "Stopping because of errors"
            return
        }

        $userlist = Get-AzureADGroupMember -ObjectId $group[0].ObjectId

        foreach ($user in $userlist) {
            if ($user.ObjectType -eq "User") {
                $null = $azureAdUsers.Add((Get-AzureADUser -ObjectId $user.ObjectId))
            }
        }
    }
    else {
        foreach ($user in $Users) {
        
            if($SkipAzureAd -eq $true)
            {
                $name = Get-LoginFromEmail $user
                $azureAdUsers.Add([PSCustomObject]@{
                    Mail = $user
                    GivenName = $name
                    DisplayName = $name
                    ObjectId = ''
                })
            }
            else
            {
                $aadUser = Get-AzureADUser -SearchString $user

                if ($null -eq $aadUser) {
                    Write-PSFMessage -Level Critical "Could not find user $user in AzureAAd"
                }
                else {
                    $null = $azureAdUsers.Add($aadUser)
                }
    

            }
        
            
        }
    }

    try {
        $sqlCommand.Connection.Open()

        foreach ($user in $azureAdUsers) {

            $identityProvider = $canonicalProvider

            Write-PSFMessage -Level Verbose -Message "Getting tenant from $($user.Mail)."
            $tenant = Get-TenantFromEmail $user.Mail

            Write-PSFMessage -Level Verbose -Message "Getting domain from $($user.Mail)."
            $networkDomain = get-NetworkDomain $user.Mail

            Write-PSFMessage -Level Verbose -Message "InstanceProvider : $InstanceProvider"
            Write-PSFMessage -Level Verbose -Message "Tenant : $Tenant"
    
            if($user.Mail.ToLower().Contains("outlook.com") -eq $true) {
                $identityProvider = "live.com"
            }
            else
            {
                if ($instanceProvider.ToLower().Contains($tenant.ToLower()) -ne $True) {
                    Write-PSFMessage -Level Verbose -Message "Getting identity provider from $($user.Mail)."
                    $identityProvider = Get-IdentityProvider $user.Mail
                }
            }
    
            Write-PSFMessage -Level Verbose -Message "Getting sid from $($user.Mail) and identity provider : $identityProvider."
            $sid = Get-UserSIDFromAad $user.Mail $identityProvider
            Write-PSFMessage -Level Verbose -Message "Generated SID : $sid"
            $id = ""
            if ($IdValue -eq 'Login') {
                $id = $IdPrefix + $(Get-LoginFromEmail $user.Mail)
            }
            else {
                $id = $IdPrefix + $user.GivenName
            }
            Write-PSFMessage -Level Verbose -Message "Id for user $($user.Mail) : $id"
    
            $name = ""
            if ($NameValue -eq 'DisplayName') {
                $name = $user.DisplayName + $NameSuffix
            }
            
            else {
                $name = $user.GivenName + $NameSuffix
            }
            Write-PSFMessage -Level Verbose -Message "Name for user $($user.Mail) : $name"
            Write-PSFMessage -Level Verbose -Message "Importing $($user.Mail) - SID $sid - Provider $identityProvider"

            Import-AadUserIntoD365FO $SqlCommand $user.Mail $name $id $sid $StartupCompany $identityProvider $networkDomain $user.ObjectId
        }
    }
    catch {
        Write-PSFMessage -Level Host -Message "Something went wrong while working against the database" -Exception $PSItem.Exception
        Stop-PSFFunction -Message "Stopping because of errors"
        return
    }
    finally {
        if ($sqlCommand.Connection.State -ne [System.Data.ConnectionState]::Closed) {
            $sqlCommand.Connection.Close()    
        }
        $sqlCommand.Dispose()
    }
}