functions/general/Deactivate-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
function Deactivate-TmfConfiguration
{
    <#
        .SYNOPSIS
            Deactivate already TMF configuration.

        .DESCRIPTION
            Deactivate configurations you don't want to apply to your tenant. Only activated configuration will be considered when testing or invoking configurations.

        .PARAMETER Name
            Provide the name of the TMF configuration to deactivate.

        .PARAMETER Path
            Provide a path to the TMF configuration to deactivate.

        .PARAMETER All
            Deactivate all configurations currently activated.

        .EXAMPLE
            PS> Deactivate-TmfConfiguration -Path "C:\Temp\SomeConfiguration"
            
            Deactivates the configuration in path C:\Temp\SomeConfiguration.
        
        .EXAMPLE
            PS> Deactivate-TmfConfiguration -Name "SomeConfiguration"
            
            Deactivates the configuration with name "SomeConfiguration".
        
        .EXAMPLE
            PS> Deactivate-TmfConfiguration -All
            
            Deactivates all configurations.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Name')]
    Param (
        [Parameter(Mandatory = $true, ParameterSetName = "Name")]
        [string] $Name,

        [Parameter(Mandatory = $true, ParameterSetName = "Path")]
        [string] $Path,

        [Parameter(ParameterSetName = "All")]
        [switch] $All
    )
    
    begin
    {
        if ($All) {
            return
        }

        if ($PSCmdlet.ParameterSetName -eq "Path") {
            if ($Path -notmatch ".*configuration.json$") {
                $configurationFilePath = "{0}\configuration.json" -f $Path    
            }

            if (!(Test-Path $configurationFilePath)) {
                Stop-PSFFunction -String "TMF.ConfigurationFileNotFound" -StringValues $configurationFilePath
                return
            }

            $configurationFilePath = Resolve-PSFPath -Provider FileSystem -Path $configurationFilePath -SingleItem
            $Name = (Get-Content $configurationFilePath | ConvertFrom-Json -ErrorAction Stop).Name
        }        
        
        if ($script:activatedConfigurations.Name -notcontains $Name) {
            Stop-PSFFunction -String "Deactivate-TMFConfiguration.NotActivated" -StringValues $Name
            return
        }
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }

        if ($All) {
            Write-PSFMessage -Level Host -String "Deactivate-TMFConfiguration.DeactivatingAll" -NoNewLine
            $script:activatedConfigurations = @()
            $script:desiredConfiguration = @{}
            Write-PSFHostColor -String ' [<c="green">DONE</c>]'
        }
        else {
            Write-PSFMessage -Level Host -String "Deactivate-TMFConfiguration.Deactivating" -StringValues $Name -NoNewLine
            $script:activatedConfigurations = @($script:activatedConfigurations | Where-Object {$_.Name -ne $Name})            
            Write-PSFHostColor -String ' [<c="green">DONE</c>]'
        }        
    }
    end
    {
        Load-TmfConfiguration
    }
}