DSCResources/MSFT_xFirefoxPreference/MSFT_xFirefoxPreference.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
156
157
158
159
160
161
162
163
164
165
166
167
$currentPath = Split-Path $MyInvocation.MyCommand.Path -Parent
Import-Module  "$currentPath\..\FirefoxPreferenceHelper.psm1" -Force

<#
    .SYNOPSIS
        Get-TargetResource returns the Firefox Install path and the current configuration
        preferences in the Mozilla.cfg file.
 
    .PARAMETER InstallDirectory
        The directory where Firefox is installed.
 
    .PARAMETER PreferenceName
        The name of the Firefox preference to configure.
#>

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $InstallDirectory,

        [Parameter(Mandatory = $true)]
        [string]
        $PreferenceName
    )

    if (-not(Test-Path -Path $InstallDirectory))
    {
        Write-Verbose -Message "$InstallDirectory not found. Verify Firefox is installed and the correct Install Directory is defined."
    }
    elseif (Test-Path -Path "$InstallDirectory\Mozilla.cfg")
    {
        $currentPreference = Get-FirefoxPreference -PreferenceName $PreferenceName -InstallDirectory $InstallDirectory -File 'Mozilla'
    }
    else
    {
        Write-Verbose -Message "No Mozilla.cfg file found"
    }

    $return = @{
        PreferenceType   = $currentPreference.PreferenceType
        PreferenceName   = $currentPreference.PreferenceName
        PreferenceValue  = $currentPreference.PreferenceValue
        InstallDirectory = $InstallDirectory
    }

    return $return
}

<#
    .SYNOPSIS
        Set-TargetResource sets Firefox config preconfigurations and preferences.
 
    .PARAMETER PreferenceName
        The name of the Firefox preference to configure.
 
    .PARAMETER PreferenceType
        The type of Firefox preference to configure.
 
    .PARAMETER PreferenceValue
        The Value of the Firefox preference to configure.
 
    .PARAMETER InstallDirectory
        The directory where Firefox is installed.
#>

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $PreferenceName,

        [Parameter()]
        [ValidateSet('Pref', 'lockPref', 'defaultPref', 'unlockPref', 'clearPref')]
        [string]
        $PreferenceType,

        [Parameter()]
        [AllowNull()]
        [string]
        $PreferenceValue,

        [Parameter(Mandatory = $true)]
        [string]
        $InstallDirectory
    )

    if (-not(Test-Path -Path $InstallDirectory))
    {
        throw "$InstallDirectory not found. Verify Firefox is installed and the correct Install Directory is defined."
    }

    $preconfigs = Test-FirefoxPreconfiguration -InstallDirectory $InstallDirectory
    if ($preconfigs)
    {
        Set-FirefoxPreconfiguration -Preconfiguration $preconfigs -InstallDirectory $InstallDirectory
    }

    if (-not(Test-Path -Path "$InstallDirectory\Mozilla.cfg"))
    {
        New-Item -Path "$InstallDirectory\Mozilla.cfg" -Type File
    }

    Set-FirefoxPreference -PreferenceName $PreferenceName -PreferenceType $PreferenceType -PreferenceValue $PreferenceValue -InstallDirectory $InstallDirectory -File 'Mozilla'
}

<#
    .SYNOPSIS
        Test-TargetResource tests Firefox config preconfigurations and Preferences.
 
    .PARAMETER PreferenceName
        The name of the Firefox preference to configure.
 
    .PARAMETER PreferenceType
        The type of Firefox preference to configure.
 
    .PARAMETER PreferenceValue
        The Value of the Firefox preference to configure.
 
    .PARAMETER InstallDirectory
        The directory where Firefox is installed.
#>

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]
        $PreferenceName,

        [Parameter()]
        [ValidateSet('Pref', 'lockPref', 'defaultPref', 'unlockPref', 'clearPref')]
        [string]
        $PreferenceType,

        [Parameter()]
        [AllowNull()]
        [string]
        $PreferenceValue,

        [Parameter(Mandatory = $true)]
        [string]
        $InstallDirectory
    )

    $preconfigurationTest = Test-FirefoxPreconfiguration -InstallDirectory $InstallDirectory

    if ($null -ne $preconfigurationTest)
    {
        Write-Verbose -Message "Firefox preferences are not set to use Mozilla.cfg"
        return $false
    }

    $inDesiredState = Test-FirefoxPreference -PreferenceName $PreferenceName -PreferenceType $PreferenceType -PreferenceValue $PreferenceValue -InstallDirectory $InstallDirectory -File 'Mozilla'

    return $inDesiredState
}

Export-ModuleMember -Function *-TargetResource