DSCResources/cFirefoxPrefs/cFirefoxPrefs.schema.psm1

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
Configuration cFirefoxPrefs
{
    # help about autoconfig
    # https://www.mozilla.jp/business/faq/tech/setting-management/
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string] $PrefName,

        [Parameter(Mandatory)]
        [AllowEmptyString()]
        [string] $PrefValue,

        [Parameter()]
        [ValidateSet('pref', 'defaultPref', 'lockPref', 'clearPref', 'LocalizablePreferences')]
        [string] $PrefType = 'pref',

        [Parameter()]
        [ValidatePattern('.+\.cfg$')]
        [string] $CfgFileName = "autoconfig.cfg",

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string] $FirefoxDirectory = "C:\Program Files\Mozilla Firefox"
    )

    Import-DscResource -ModuleName PSDesiredStateConfiguration
    Import-DscResource -ModuleName DSCR_FileContent

    $MozIniPath = Join-Path $FirefoxDirectory "\distribution\distribution.ini"
    $MozPrefJsPath = Join-Path $FirefoxDirectory "\defaults\pref\autoconfig.js"
    $MozPrefCfgPath = Join-Path $FirefoxDirectory $CfgFileName

    $FormattedPrefValue = & {
        if ([float]::TryParse($PrefValue, [ref]$null)) {$PrefValue}
        elseif ([bool]::TryParse($PrefValue, [ref]$null)) {$PrefValue.toLower()}
        else {"`"$PrefValue`""}
    }

    $PrefLine = if ($PrefType -eq 'clearPref') {
        ('{0}("{1}");' -f $PrefType, $PrefName)
    }
    else {
        ('{0}("{1}", {2});' -f $PrefType, $PrefName, $FormattedPrefValue)
    }

    $Global = @{
        id      = 'DSC-Customized'
        version = '1.0'
        about   = 'DSC-Customized'
    }

    Script Test_FireFoxDirectory
    {
        GetScript = {
        }
        TestScript = {
            if(-not (Test-Path (Join-Path $using:FirefoxDirectory 'FireFox.exe') -PathType Leaf)){
                Write-Warning ('"FireFox.exe" does not exist in "{0}". Please confirm FireFoxDirectory' -f $using:FirefoxDirectory)
            }
            $true
        }
        SetScript = {
        }
    }

    if (($PrefType -eq 'pref') -or ($PrefType -eq 'LocalizablePreferences')) {
        IniFile Global_Id
        {
            Ensure   = 'Present'
            Path     = $MozIniPath
            Key      = 'id'
            Value    = $Global.id
            Section  = 'Global'
            Encoding = 'UTF8'
        }
        IniFile Global_version
        {
            Ensure   = 'Present'
            Path     = $MozIniPath
            Key      = 'version'
            Value    = $Global.version
            Section  = 'Global'
            Encoding = 'UTF8'
        }
        IniFile Global_about
        {
            Ensure   = 'Present'
            Path     = $MozIniPath
            Key      = 'about'
            Value    = $Global.about
            Section  = 'Global'
            Encoding = 'UTF8'
        }
    }

    if ($PrefType -eq 'pref') {
        IniFile Prefs
        {
            Ensure   = 'Present'
            Path     = $MozIniPath
            Key      = $PrefName
            Value    = $FormattedPrefValue
            Section  = 'Preferences'
            Encoding = 'UTF8'
        }
    }
    elseif ($PrefType -eq 'LocalizablePreferences') {
        IniFile LocalizedPrefs
        {
            Ensure   = 'Present'
            Path     = $MozIniPath
            Key      = $PrefName
            Value    = $FormattedPrefValue
            Section  = 'LocalizablePreferences'
            Encoding = 'UTF8'
        }
    }
    else {
        File autoconfig_js {
            DestinationPath = $MozPrefJsPath
            Contents        = @"
pref("general.config.filename", "$CfgFileName");
pref("general.config.vendor", "autoconfig");
pref("general.config.obscure_value", 0);
"@

        }

        Script autoconfig_cfg {
            SetScript  = {
                if (-not (Test-Path $using:MozPrefCfgPath)) {
                    New-Item -Path $using:MozPrefCfgPath -ItemType File -Force
                    '//The first line must be a comment' | Out-File $using:MozPrefCfgPath -Encoding utf8
                }
                (gc $using:MozPrefCfgPath) -replace ('(lock|clear|default|)pref\(\s*"{0}"\s*,\s*.+\s*\)\s*;' -f $using:PrefName), '' | Out-File $using:MozPrefCfgPath -Encoding utf8
                $using:PrefLine | Out-File $using:MozPrefCfgPath -Append -Encoding utf8
            }
            TestScript = {
                $RegExp = ('{0}\(\s*"{1}"\s*,\s*{2}\s*\)\s*;' -f $using:PrefType, $using:PrefName, $using:FormattedPrefValue)
                (Test-Path $using:MozPrefCfgPath) -and
                (get-item $using:MozPrefCfgPath | Select-String -Pattern $RegExp)
            }
            GetScript  = {
                $RegExp = ('{0}\(\s*"{1}"\s*,\s*{2}\s*\)\s*;' -f $using:PrefType, $using:PrefName, $using:FormattedPrefValue)
                @{
                    TestScript = $TestScript
                    SetScript  = $SetScript
                    GetScript  = $GetScript
                    Result     = ((Test-Path $using:MozPrefCfgPath) -and (get-item $using:MozPrefCfgPath | Select-String -Pattern $RegExp))
                }
            }
        }
    }
}