internal/functions/new-d365foaadapplication.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
52
53
54
55
56
57
58

<#
    .SYNOPSIS
        Creates a new Azure Active Directory (AAD) application
         
    .DESCRIPTION
        Creates a new Azure Active Directory (AAD) application in a Dynamics 365 for Finance & Operations instance
         
    .PARAMETER sqlCommand
        The SQL Command object that should be used when creating the new application
         
    .PARAMETER Name
        The name that the imported application should have inside the D365FO environment
         
    .PARAMETER UserId
        The id of the user linked to the application inside the D365FO environment
         
    .PARAMETER ClientId
        The Client ID that the imported application should use inside the D365FO environment
         
    .EXAMPLE
        PS C:\> $SqlCommand = Get-SqlCommand -DatabaseServer localhost -DatabaseName AxDB -SqlUser User123 -SqlPwd "Password123"
        PS C:\> New-D365FOAadApplication -SqlCommand $SqlCommand -Name "Application1" -UserId "admin" -ClientId "aef2e67c-64a3-4c72-9294-d288c5bf503d"
        This will get a SqlCommand object that will connect to the localhost server and the AXDB database, with the sql credential "User123".
        The SqlCommand object is passed to the New-D365FOAadApplication along with all the necessary details for importing Application1 as an application linked to user admin into the D365FO environment.
         
    .NOTES
        Author: Gert Van Der Heyden (@gertvdheyden)
         
#>

function New-D365FOAadApplication {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    Param (
        [System.Data.SqlClient.SqlCommand] $SqlCommand,

        [string] $Name,

        [string] $UserId,

        [string] $ClientId
    )
    
    $sqlCommand.CommandText = (Get-Content "$script:ModuleRoot\internal\sql\Add-AadApplicationIntoD365FO.sql") -join [Environment]::NewLine

    Write-PSFMessage -Level Verbose -Message "Adding Application : $Name,$UserId,$ClientId"
    
    $null = $sqlCommand.Parameters.Add("@Name", $Name)
    $null = $sqlCommand.Parameters.Add("@UserId", $UserId)
    $null = $sqlCommand.Parameters.Add("@ClientId", $ClientId)

    Write-PSFMessage -Level Verbose -Message "Creating the application in database"

    Write-PSFMessage -Level InternalComment -Message "Executing a script against the database." -Target (Get-SqlString $SqlCommand)

    $null = $sqlCommand.ExecuteNonQuery()
    
    Write-PSFMessage -Level Verbose -Message "Added application"
}