DSCResources/cFirefoxBookmarksPolicy/cFirefoxBookmarksPolicy.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
Configuration cFirefoxBookmarksPolicy
{
    # 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] $Title,

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

        [Parameter(Mandatory = $false)]
        [string] $Favicon,

        [Parameter(Mandatory = $false)]
        [ValidateSet('toolbar', 'menu')]
        [string] $Placement,

        [Parameter(Mandatory = $false)]
        [string] $Folder,

        [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"

    $BookmarksPolicyParam = @{
        Title = $Title
        URL   = $URL
    }

    if ($Favicon) {
        $BookmarksPolicyParam.Add('Favicon', $Favicon)
    }

    if ($Placement) {
        $BookmarksPolicyParam.Add('Placement', $Placement)
    }

    if ($Folder) {
        $BookmarksPolicyParam.Add('Folder', $Folder)
    }

    $FormattedPolicyValue = ConvertTo-Json -InputObject $BookmarksPolicyParam -Compress

    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 BookmarksPolicy {
        Ensure = $Ensure
        Path   = $MozPolicyPath
        Key    = "policies/Bookmarks"
        Value  = $FormattedPolicyValue
        Mode   = 'ArrayElement'
    }
}