functions/disable-d365flight.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

<#
    .SYNOPSIS
        Used to disable a flight
         
    .DESCRIPTION
        Provides a method for disabling a flight in D365FO.
         
    .PARAMETER FlightName
        Name of the flight to disable
         
    .PARAMETER DatabaseServer
        The name of the database server
         
        If on-premises or classic SQL Server, use either short name og Fully Qualified Domain Name (FQDN)
         
        If Azure use the full address to the database server, e.g. server.database.windows.net
         
    .PARAMETER DatabaseName
        The name of the database
         
    .PARAMETER SqlUser
        The login name for the SQL Server instance
         
    .PARAMETER SqlPwd
        The password for the SQL Server user
         
    .EXAMPLE
        PS C:\> Disable-D365Flight -FlightName DMFEnableAllCompanyExport
         
        Disables the flight DMFEnableAllCompanyExport
         
    .LINK
        https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/data-entities-data-packages#features-flighted-in-data-management-and-enabling-flighted-features
         
    .NOTES
        Tags: Flight, Flighting
         
        Author: Frank Hüther (@FrankHuether)
         
        The DataAccess.FlightingServiceCatalogID must already be set in the web.config file.
         
        At no circumstances can this cmdlet be used to enable a flight in a PROD environment.
#>

function Disable-D365Flight {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 1)]
        [String] $FlightName,

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

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

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

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

    try {
        $WebConfigFile = join-Path -path $Script:AOSPath $Script:WebConfig
        Write-PSFMessage -Level Verbose -Message "Retrieve the FlightingServiceCatalogID" -Target $WebConfigFile

        $FlightServiceNode = Select-Xml -XPath "/configuration/appSettings/add[@key='DataAccess.FlightingServiceCatalogID']/@value" -Path $WebConfigFile
        $FlightServiceId = $FlightServiceNode.Node.Value
    
        Write-PSFMessage -Level Verbose -Message "FlightingServiceCatalogID: $FlightServiceId" -Target $WebConfigFile
    }
    catch {
        Write-PSFMessage -Level Host -Message "Something went wrong while reading from the web.config file" -Exception $PSItem.Exception
        Stop-PSFFunction -Message "Stopping because of errors"
        return
    }

    if ($null -eq $FlightServiceId) {
        Write-PSFMessage -Level Host -Message "The DataAccess.FlightingServiceCatalogID setting must be set in the web.config file. See https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/data-entities-data-packages#features-flighted-in-data-management-and-enabling-flighted-features for details"
        Stop-PSFFunction -Message "Stopping because of errors"
        return
    }

    $UseTrustedConnection = Test-TrustedConnection $PSBoundParameters

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

    $SqlCommand = Get-SqlCommand @SqlParams -TrustedConnection $UseTrustedConnection
    $sqlCommand.CommandText = (Get-Content "$script:ModuleRoot\internal\sql\disable-flight.sql") -join [Environment]::NewLine

    try {
        $sqlCommand.Connection.Open()

        Write-PSFMessage -Level Verbose -Message "Disabling flight: $FlightName"

        $null = $sqlCommand.Parameters.Add("@FlightName", $FlightName)
        $null = $sqlCommand.Parameters.Add("@FlightServiceId", $FlightServiceId)

        Write-PSFMessage -Level Verbose -Message "Disable the flight 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 "Flight $FlightName disabled with service ID $FlightServiceId"
    }
    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()
    }
}