DSCResources/cFirefoxPolicy/cFirefoxPolicy.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
Configuration cFirefoxPolicy
{
    # help about Firefox Policy Engine
    # https://github.com/mozilla/policy-templates
    param
    (
        [Parameter(Mandatory = $false)]
        [ValidateSet('Ensure', 'Absent')]
        [string] $Ensure = 'Present',

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $PolicyName,

        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string] $PolicyValue,

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

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

    $MozPolicyPath = Join-Path $FirefoxDirectory "\distribution\policies.json"

    $FormattedPolicyValue = & {
        if ([float]::TryParse($PolicyValue, [ref]$null)) {$PolicyValue}
        elseif ([bool]::TryParse($PolicyValue, [ref]$null)) {$PolicyValue.toLower()}
        else {
            [string]$tmp =
            if($PolicyValue.Trim() -notmatch '^[\[{"].+[\]}"]$'){
                '"' + $PolicyValue.Trim() + '"'
            }
            else{
                $PolicyValue.Trim()
            }

            try {
                ConvertFrom-Json -InputObject $tmp -ErrorAction Stop >$null
                $tmp
            }
            catch {
                throw [System.ArgumentException]::new('The PolicyValue should be valid JSON formatted string.')
            }
        }
    }

    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 = {
        }
    }

    JsonFile FirefoxPolicy
    {
        Ensure  = $Ensure
        Path    = $MozPolicyPath
        Key     = "policies/$PolicyName"
        Value   = $FormattedPolicyValue
    }
}