DSCResources/MSFT_AuditPolicyOption/MSFT_AuditPolicyOption.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Import-Module -Name (Join-Path -Path ( Split-Path $PSScriptRoot -Parent ) `
                               -ChildPath 'AuditPolicyResourceHelper\AuditPolicyResourceHelper.psm1') `
                               -Force

# Localized messages for Write-Verbose statements in this resource
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_AuditPolicyOption'

<#
    .SYNOPSIS
        Gets the value of the audit policy option.
    .PARAMETER Name
        Specifies the option to get.
    .PARAMETER Value
        Not used in Get-TargetResource.
#>

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('CrashOnAuditFail', 'FullPrivilegeAuditing', 'AuditBaseObjects',
        'AuditBaseDirectories')]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Enabled', 'Disabled')]
        [String]
        $Value
    )

    # Get the option's current value
    $optionValue = Get-AuditOption -Name $Name

    Write-Verbose -Message ( $localizedData.GetAuditpolOptionSucceed -f $Name )

    return @{
        Name  = $Name
        Value = $optionValue
    }
}

<#
    .SYNOPSIS
        Sets the value of the audit policy option.
    .PARAMETER Name
        Specifies the option to set.
    .PARAMETER Value
        Specifies the value to set the option to.
#>

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('CrashOnAuditFail', 'FullPrivilegeAuditing', 'AuditBaseObjects',
        'AuditBaseDirectories')]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Enabled', 'Disabled')]
        [String]
        $Value
    )

    try
    {
        Set-AuditOption -Name $Name -Value $Value
        Write-Verbose -Message ( $localizedData.SetAuditpolOptionSucceed -f $Name, $Value )
    }
    catch
    {
        Write-Verbose -Message ( $localizedData.SetAuditpolOptionFailed -f $Name, $Value )
    }
}

<#
    .SYNOPSIS
        Tests that the audit policy option is in the desired state
    .PARAMETER Name
        Specifies the option to test.
    .PARAMETER Value
        Specifies the value to test against the option.
#>

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('CrashOnAuditFail', 'FullPrivilegeAuditing', 'AuditBaseObjects',
        'AuditBaseDirectories')]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Enabled', 'Disabled')]
        [String]
        $Value
    )

    if ( ( Get-AuditOption -Name $Name ) -eq $Value )
    {
        Write-Verbose -Message ( $localizedData.TestAuditpolOptionCorrect -f $Name, $value )
        return $true
    }
    else
    {
        Write-Verbose -Message ( $localizedData.TestAuditpolOptionIncorrect -f $Name, $value )
        return $false
    }
}

#---------------------------------------------------------------------------------------------------
# Support functions to handle auditpol I/O

<#
    .SYNOPSIS
        Gets the audit policy option state.
    .DESCRIPTION
        Ths is one of the public functions that calls into Get-AuditOptionCommand.
        This function enforces parameters that will be passed through to the
        Get-AuditOptionCommand function and aligns to a specifc parameterset.
    .PARAMETER Option
        The name of an audit option.
    .OUTPUTS
        A string that is the state of the option (Enabled|Disables).
#>

function Get-AuditOption
{
    [CmdletBinding()]
    [OutputType([String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $Name
    )
    <#
        When PowerShell cmdlets are released for individual audit policy settings a condition
        will be placed here to use native PowerShell cmdlets to set the option details.
    #>

    # Get the converted auditpol csv object
    $auditOption = Invoke-AuditPol -Command "Get" -SubCommand "Option:$Name"

    # The option value is stored in the 'Inclusion Setting' property of the output CSV.
    return $auditOption.'Inclusion Setting'
}

<#
    .SYNOPSIS
        Sets an audit policy option to enabled or disabled.
    .DESCRIPTION
        This public function calls Set-AuditOptionCommand and enforces parameters
        that will be passed to Set-AuditOptionCommand and aligns to a specifc parameterset.
    .PARAMETER Name
        The specific option to set.
    .PARAMETER Value
        The value to set the provided option to.
#>

function Set-AuditOption
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [String]
        $Value
    )

    <#
        When PowerShell cmdlets are released for individual audit policy settings a condition
        will be placed here to use native PowerShell cmdlets to set the option details.
    #>

    if ( $pscmdlet.ShouldProcess( "$Name","Set $Value" ) )
    {
        <#
            The output text of auditpol is in simple past tense, but the input is in simple
            present tense, so the hashtable converts the input accordingly.
        #>

        $pastToPresentValues = @{
            'Enabled'  = 'enable'
            'Disabled' = 'disable'
        }

        [String[]] $subCommand = @( "Option:$Name", "/value:$($pastToPresentValues[$value])" )

        Invoke-AuditPol -Command 'Set' -SubCommand $subCommand | Out-Null
    }
}

Export-ModuleMember -Function *-TargetResource