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

<#
    .SYNOPSIS
        Get AX 2012 environment details from the configuration store
         
    .DESCRIPTION
        Get the environment details for the AX 2012 environment(s) that are stored in the configuration store
         
    .PARAMETER Name
        Name of the configuration that you want to work against
         
    .PARAMETER OutputAsHashtable
        Instruct the cmdlet to return a hastable object
         
    .PARAMETER EnableException
        This parameters disables user-friendly warnings and enables the throwing of exceptions
        This is less user friendly, but allows catching exceptions in calling scripts
         
    .EXAMPLE
        PS C:\> Get-AxEnvironmentConfig
         
        This will get all saved environment configurations.
         
    .NOTES
        Tags: Servicing, Environment, Config, Configuration, Servers
         
        Author: Mötz Jensen (@Splaxi)
         
    .LINK
        Add-AxEnvironmentConfig
         
    .LINK
        Get-AxActiveEnvironmentConfig
         
    .LINK
        Set-AxActiveEnvironmentConfig
         
#>


function Get-AxEnvironmentConfig {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '')]
    [CmdletBinding()]
    [OutputType('PSCustomObject')]
    param (
        [string] $Name = "*",

        [switch] $OutputAsHashtable,

        [switch] $EnableException
    )
    
    Write-PSFMessage -Level Verbose -Message "Fetch all configurations based on $Name" -Target $Name

    $Name = $Name.ToLower()
    $configurations = Get-PSFConfig -FullName "ax2012.tools.environment.$Name.name"

    if($($configurations.count) -lt 1) {
        $messageString = "No configurations found <c='em'>with</c> the name."
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because no configuration found." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>','')))
        return
    }
    
    foreach ($configName in $configurations.Value.ToLower()) {
        Write-PSFMessage -Level Verbose -Message "Working against the $configName configuration" -Target $configName
        $res = @{}

        $configName = $configName.ToLower()

        foreach ($config in Get-PSFConfig -FullName "ax2012.tools.environment.$configName.*") {
            Write-PSFMessage -Level Verbose -Message "Working against the $($config.FullName.ToString()) of $configName configuration" -Target $configName

            $propertyName = (Get-Culture).TextInfo.ToTitleCase($config.FullName.ToString().Replace("ax2012.tools.environment.$configName.", "")).Replace("servers","Servers")
            $res.$propertyName = $config.Value
        }
        
        if($OutputAsHashtable) {
            $res
        } else {
            [PSCustomObject]$res | Select-PSFObject -TypeName "AX2012.TOOLS.Environment.Config"
        }
    }
}