functions/general/Activate-TmfConfiguration.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
function Activate-TmfConfiguration
{
    <#
        .SYNOPSIS
            Activates already created TMF configurations.

        .DESCRIPTION
            Activate configurations you want to apply to your tenant. An activated configuration will be considered when testing or invoking configurations.

        .PARAMETER ConfigurationPaths
            One or more paths to Tenant Management Framework configuration directories can be provided.

        .PARAMETER Force
            Overwrite already loaded configurations.

        .PARAMETER DoNotLoad
            Do not load resource definitions after activating.

        .EXAMPLE
            PS> Activate-TmfConfiguration -ConfigurationPaths "C:\Temp\SomeConfiguration"
            
            Activates the configuration in path C:\Temp\SomeConfiguration.

    #>

    [CmdletBinding()]
    Param (
        [string[]] $ConfigurationPaths,
        [switch] $Force,
        [switch] $DoNotLoad
    )
    
    begin
    {
        $configurationsToActivate = @()
        #region Prepare to activate
        foreach ($path in $ConfigurationPaths) {
            $configuration = [PSCustomObject] @{
                filePath = "{0}\configuration.json" -f $path
                directoryPath = $path
                alreadyActivated = $false
            }

            #region Check if configuration.json exists
            if (!(Test-Path $configuration.filePath)) {
                Stop-PSFFunction -String "TMF.ConfigurationFileNotFound" -StringValues $configuration.filePath
                return
            }
            else {
                # Clean configuration path
                $configuration.filePath = Resolve-PSFPath -Provider FileSystem -Path $configuration.filePath -SingleItem
            }
            #endregion

            #region Load configuration.json
            $contentDummy = Get-Content $configuration.filePath | ConvertFrom-Json -ErrorAction Stop
            $contentDummy | Get-Member -MemberType NoteProperty | ForEach-Object {
                Add-Member -InputObject $configuration -MemberType NoteProperty -Name $_.Name -Value $contentDummy.$($_.Name)
            }
            $configuration.alreadyActivated = $script:activatedConfigurations.Name -contains $configuration.Name
            #endregion
            
            if (!$Force -and $configuration.alreadyActivated) {
                Write-PSFMessage -Level Warning -String "Activate-TMFConfiguration.AlreadyActivated" -StringValues $configuration.Name, $configuration.filePath
            }

            $configurationsToActivate += $configuration
        }
        #endregion
        
        #region Check prerequisites
        foreach ($configuration in $configurationsToActivate) {            
            if ($configuration.Prerequisite.Count -gt 0) {                
                foreach ($prereq in $configuration.Prerequisite) {
                    $prereqMet = $false
                    if ($prereq.contains("||")) {                        
                        $prereq.split("||") | Foreach-Object {
                            if ($_.trim() -in $configurationsToActivate.Name -or $_.trim() -in (Get-TmfActiveConfiguration).Name) { $prereqMet = $true }
                        }
                    }
                    else {
                        if ($prereq -in $configurationsToActivate.Name -or $prereq -in (Get-TmfActiveConfiguration).Name) {
                            $prereqMet = $true
                        }
                    }
                    if (-Not $prereqMet) {
                        Stop-PSFFunction -String "TMF.PrerequisiteNotActivated" -StringValues $configuration.Name, $prereq
                        return
                    }
                }
            }
        }
        #endregion
    }
    process
    {        
        if (Test-PSFFunctionInterrupt) { return }

        foreach ($configuration in $configurationsToActivate) {
            if ($Force -and $configuration.alreadyActivated) {
                Write-PSFMessage -Level Host -String "Activate-TMFConfiguration.RemovingAlreadyLoaded" -StringValues $configuration.Name, $configuration.directoryPath -NoNewLine
                $script:activatedConfigurations = @($script:activatedConfigurations | Where-Object {$_.Name -ne $configuration.Name})
                Write-PSFHostColor -String ' [<c="green">DONE</c>]'
            }
            elseif (!$Force -and $configuration.alreadyActivated) {
                continue
            }

            Write-PSFMessage -Level Host -String "Activate-TMFConfiguration.Activating" -StringValues $configuration.Name, $configuration.filePath -NoNewLine            
            $script:activatedConfigurations += $configuration | Select-Object Name, @{Name = "Path"; Expression = {Resolve-Path $_.directoryPath}}, Description, Author, Weight, Prerequisite
            Write-PSFHostColor -String ' [<c="green">DONE</c>]'
        }

        Write-PSFMessage -Level Host -String "Activate-TMFConfiguration.Sort" -NoNewLine
        $script:activatedConfigurations = @($script:activatedConfigurations | Sort-Object Weight)
        Write-PSFHostColor -String ' [<c="green">DONE</c>]'
    }
    end
    {
        if (-Not $DoNotLoad) {
            Load-TmfConfiguration
        }
    }
}