Configuration.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

function Import-EnvironmentModuleCoreConfiguration
{
    <#
    .SYNOPSIS
    Import the configuration from the given file.
    .PARAMETER ConfigurationFile
    The configuration file to read. The content type must be XML.
    #>

    [CmdletBinding(ConfirmImpact='Low', SupportsShouldProcess=$true)]
    param(
        [String] $ConfigurationFile
    )
    process {
        if ( -not $PSCmdlet.ShouldProcess("Import environment module configuration")) {
            return
        }

        if(-not (Test-Path $ConfigurationFile)) {
            return
        }
        $script:configuration = Import-Clixml $ConfigurationFile
    }
}

function Export-EnvironmentModuleCoreConfiguration
{
    <#
    .SYNOPSIS
    Export the internal configuration to a file.
    .PARAMETER ConfigurationFile
    The configuration file to write. The content type will be XML.
    #>

    [CmdletBinding()]
    param(
        [String] $ConfigurationFile = $null
    )
    process {
        if([string]::IsNullOrEmpty($ConfigurationFile)) {
            $ConfigurationFile = $script:configurationFilePath
        }

        $script:configuration | Export-Clixml $ConfigurationFile
    }
}

function Set-EnvironmentModuleConfigurationValue
{
    <#
    .SYNOPSIS
    Set a configuration value influencing the behaviour of the environment module engine. The configuration value is stored persistently.
    .PARAMETER ParameterName
    The name of the configuration parameter to set.
    .PARAMETER Value
    The value set.
    #>

    [CmdletBinding(ConfirmImpact='Low', SupportsShouldProcess=$true)]
    param(
    )
    DynamicParam {
        $runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $moduleSet = @("DefaultModuleStoragePath", "ShowLoadingMessages", "CreateDefaultModulesByArchitecture",
                       "CreateDefaultModulesByName", "CreateDefaultModulesByMajorVersion")
        Add-DynamicParameter 'ParameterName' String $runtimeParameterDictionary -Mandatory $True -Position 0 -ValidateSet $moduleSet
        Add-DynamicParameter 'Value' String $runtimeParameterDictionary -Mandatory $True -Position 1

        return $runtimeParameterDictionary
    }
    begin {
        # Bind the parameter to a friendly variable
        $ParameterName = $PsBoundParameters["ParameterName"]
        $Value = $PsBoundParameters["Value"]
    }
    process {
        if ( -not $PSCmdlet.ShouldProcess("Change environment module configuration")) {
            return
        }

        $script:configuration[$ParameterName] = $Value
        Export-EnvironmentModuleCoreConfiguration
    }
}