functions/add-axenvironmentconfig.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

<#
    .SYNOPSIS
        Add configuration details for an entire AX 2012 environment
         
    .DESCRIPTION
        Build a configuration containing all the different servers / machines that is part of any given AX 2012 environment
         
        You could register your TEST, SAT, UAT, PROD environment and easily switch between them when you want to troubleshoot or run maintenance work against them
         
    .PARAMETER Name
        Name of the environment that you are adding
         
    .PARAMETER AosServers
        Array with server names of all the servers that host an AOS instance in the specific environment
         
    .PARAMETER InstanceName
        Name of the instance that is used to uniquely identify the environment across multiple AOS instances
         
    .PARAMETER DatabaseServers
        Array with server names of all the servers that host a SQL Server database for the environment
         
    .PARAMETER Database
        Database name for the SQL Server database that the AOS instance(s) connects to
         
    .PARAMETER ModelstoreDatabase
        Database name for the SQL Server database that holds the modelstore (code)
         
    .PARAMETER SsrsServers
        Array with server names of all the servers that host a Sql Server Reporting Services (SSRS) instance in the specific environment
         
    .PARAMETER EpServers
        Array with server names of all the servers that host a SharePoint installation with corresponding Enterprise Portal components in the specific environment
         
    .PARAMETER WmdpServers
        Array with server names of all the servers that host an IIS installation with corresponding Warehouse Mobile Device Portal (WMDP) components in the specific environment
         
    .PARAMETER Mr2012Servers
        Array with server names of all the servers that host a Management Reporter 2012 instance in the specific environment
         
    .PARAMETER SsasServers
        Array with server names of all the servers that host a Sql Server Analysis Services (SSAS) instance in the specific environment
         
    .PARAMETER Append
        Instructs the cmdlet to append the different parameter values with those that might already exist in the configuration store
         
    .PARAMETER Force
        Instruct the cmdlet to overwrite the specified parameter values in the configuration store
         
    .PARAMETER Temporary
        Instruct the cmdlet to only temporarily add the environment configuration in the configuration store
         
        Great help while building the configuration and you don't want to persist the configuration on the machine
         
    .EXAMPLE
        PS C:\> Add-AxEnvironmentConfig -Name AXTEST -AosServers TESTAOS01 -InstanceName AXTEST -DatabaseServers TESTSQL01 -Database AXTEST -ModelstoreDatabase AXTEST_model -Temporary
         
        This adds a new environment configuration to the configuration store.
        The Name AXTEST is used as the name for the configuration of the environment.
        The InstanceName AXTEST is used as the instance name for the configuration of the environment.
        The server TESTAOS01 is registered as the AOS Server.
        The server TESTSQL01 is registered as the SQL Server.
        The database AXTEST is registered as the SQL Server database.
        The database AXTEST_model is registered as the SQL Server database for the modelstore.
         
    .NOTES
        Tags: Environment, Config, Configuration, Servers
         
        Author: Mötz Jensen (@Splaxi)
         
    .LINK
        Clear-AxActiveEnvironmentConfig
         
    .LINK
        Get-AxActiveEnvironmentConfig
         
    .LINK
        Get-AxEnvironmentConfig
         
    .LINK
        Remove-AxEnvironmentConfig
         
    .LINK
        Set-AxActiveEnvironmentConfig
#>


function Add-AxEnvironmentConfig {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string] $Name,

        [string[]] $AosServers,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $InstanceName,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias('DatabaseServer')]
        [string[]] $DatabaseServers,

        [Alias('DatabaseName')]
        [string] $Database,

        [string] $ModelstoreDatabase,

        [string[]] $SsrsServers,

        [string[]] $EpServers,

        [string[]] $WmdpServers,

        [string[]] $Mr2012Servers,

        [string[]] $SsasServers,

        [switch] $Append,

        [switch] $Force,

        [switch] $Temporary
    )
    
    Write-PSFMessage -Level Verbose -Message "Testing if configuration with the name already exists or not." -Target $configurationValue

    if (((Get-PSFConfig -FullName "ax2012.tools.environment.*.name").Value -contains $Name) -and (-not $Force) -and (-not $Append)) {
        $messageString = "An environment configuration with <c='em'>$Name</c> as name <c='em'>already exists</c>. If you want to <c='em'>overwrite</c> the current configuration, please supply the <c='em'>-Force</c> parameter. If you want to <c='em'>append</c> the current configuration, please supply the <c='em'>-Append</c> parameter"
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because an environment configuration already exists with that name." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
        return
    }

    $configName = $Name.ToLower()

    #The ':keys' label is used to have a continue inside the switch statement itself
    :keys foreach ($key in $PSBoundParameters.Keys) {
        
        $configurationValue = $PSBoundParameters.Item($key)
        $configurationName = $key.ToLower()
        $fullConfigName = ""

        Write-PSFMessage -Level Verbose -Message "Working on $key with $configurationValue" -Target $configurationValue
        
        switch ($key) {
            "Name" {
                $fullConfigName = "ax2012.tools.environment.$configName.name"
            }

            { "Temporary", "Force", "Append", "Verbose" -contains $_ } {
                continue keys
            }
            
            Default {
                $fullConfigName = "ax2012.tools.environment.$configName.$configurationName"
            }
        }

        if ($Append -and $key -ne "Name") {
            $oldValue = @(Get-PsfConfigValue -FullName $fullConfigName)
            $temp = @()

            if($null -ne $oldValue) {
                $temp += $oldValue
            }

            $temp += $configurationValue

            $configurationValue = $temp
        }

        Write-PSFMessage -Level Verbose -Message "Setting $fullConfigName to $configurationValue" -Target $configurationValue
        
        Set-PSFConfig -FullName $fullConfigName -Value $configurationValue
        
        if (-not $Temporary) { Register-PSFConfig -FullName $fullConfigName -Scope UserDefault }
    }
}