Public/Get-GPOComment.ps1

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
#requires -Modules GroupPolicy

function Get-GPOComment {
    <#
    .SYNOPSIS
        Retrieve comments/descriptions embedded in GPOs
    .DESCRIPTION
        Retrieve comments and descriptions embedded in GPOs, GPO Settings and GP Preferences Settings
    .PARAMETER GPOName
        Name(s) of Group Policy Objects or '*' for all GPOs
    .PARAMETER PolicyGroup
        What aspects of each GPO is to be queried
        List = Policy, Settings, Preferences
    .EXAMPLE
        Get-GPOComment -GPOName '*' -PolicyGroup 'Policy'
    .EXAMPLE
        $GpoNames | Get-GPOComment -PolicyGroup 'Policy'
    .EXAMPLE
        Get-GPOComment -GPOName '*' -PolicyGroup 'Settings'
    .EXAMPLE
        Get-GPOComment -GPOName '*' -PolicyGroup 'Preferences'
    .NOTES
        version 1.1.2 - 1/3/2018 - David Stein
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory = $True, ValueFromPipeline = $True, HelpMessage = 'Name of Policy or Policies')]
            [ValidateNotNullOrEmpty()]
            [string[]] $GPOName,
        [parameter(Mandatory = $True, HelpMessage = 'Policy group to query')]
            [ValidateSet('Policy', 'Settings', 'Preferences')]
            [string] $PolicyGroup,
        [parameter(Mandatory = $False)]
            [switch] $ShowInfo
    )
    if ($ShowInfo) {
        $ModuleData = Get-Module GPODoc
        $ModuleVer  = $ModuleData.Version -join '.'
        Write-Host "GPODoc $ModuleVer - https://github.com/Skatterbrainz/GPODoc" -ForegroundColor Cyan
    }
    switch ($PolicyGroup) {
        'Policy' {
            if ($GPOName -eq '*') {
                Write-Verbose "loading all policy objects"
                $gpos = Get-GPO -All | Sort-Object -Property DisplayName
            }
            else {
                Write-Verbose "loading specific policy objects"
                try {
                    $gpos = $GPOName | Foreach-Object {Get-GPO -Name $_ -ErrorAction Stop | Select-Object Id, DisplayName, DomainName, Description}
                }
                catch {
                    Write-Warning "Unable to load GPO"
                    break
                }
            }
            foreach ($gpo in $gpos) {
                Write-Verbose "policy: $($gpo.DisplayName)"
                $data = [ordered]@{
                    PolicyID    = $gpo.Id
                    Name        = $gpo.DisplayName
                    Description = $gpo.Description
                }
                New-Object -TypeName PSObject -Property $data
            }
            break
        }
        'Settings' {
            if ($GPOName -eq '*') {
                Write-Verbose "loading all policy objects"
                $gpos = Get-GPO -All | Sort-Object -Property DisplayName
            }
            else {
                Write-Verbose "loading specific policy objects"
                $gpos = $GPOName | 
                    Foreach-Object {Get-GPO -Name $_ -ErrorAction Stop | Select-Object Id, DisplayName, DomainName, Description}
            }
            foreach ($gpo in $gpos) {
                $configs = @('Machine', 'User')
                Write-Verbose "policy: $($gpo.DisplayName)"
                foreach ($config in $configs) {
                    $commFile = Get-GpoCommentFile -Gpo $gpo -ConfigType $config
                    if ($commFile -ne "") {
                        Write-Verbose "loading... $commFile"
                        [xml]$cmtx = Get-Content -Path $commFile
                        $clinks = $cmtx.policyComments.resources.stringTable.string
                        foreach ($clink in $clinks) {
                            $data = [ordered]@{
                                PolicyID      = $gpo.Id
                                Name          = $gpo.DisplayName
                                Configuration = $config
                                Setting       = $clink.Id
                                Comment       = $clink.InnerText.Trim()
                            }
                            New-Object -TypeName PSObject -Property $data
                        }
                    }
                    else {
                        Write-Verbose "there are no $config comments in $($gpo.DisplayName)"
                        $data = [ordered]@{
                            PolicyID      = $gpo.ID
                            Name          = $gpo.DisplayName
                            Configuration = $config
                            Setting       = $null
                            Comment       = $null
                        }
                        New-Object -TypeName PSObject -Property $data
                    }
                }
            }
            break
        }
        'Preferences' {
            if ($GPOName -eq '*') {
                Write-Verbose "loading all policy objects: preferences"
                $gpos = Get-GPO -All | Sort-Object -Property DisplayName
            }
            else {
                Write-Verbose "loading specific policy objects"
                $gpos = $GPOName | Foreach-Object {Get-GPO -Name $_ -ErrorAction Stop | Select-Object Id, DisplayName, DomainName, Description}
            }
            foreach ($gpo in $gpos) {
                $configs = @('Machine', 'User')
                Write-Verbose "policy: $($gpo.DisplayName)"
                foreach ($config in $configs) {
                    $gppPath = Get-GppCommentPath -Gpo $gpo -ConfigType $config
                    if ($gppPath -ne "") {
                        Write-Verbose "there are preferences for this policy object"
                        foreach ($section in Get-ChildItem -Path $gppPath -Directory | Select-Object -ExpandProperty Name) {
                            $gppFile = "$gppPath\$section\$section.xml"
                            if (Test-Path $gppFile) {
                                Write-Verbose "gppref file: $gppFile"
                                [xml]$gppXML = Get-Content $gppFile
                                $Element = $section.substring(0, $section.Length - 1)
                                foreach ($Element in $gppXML."$section"."$Element") {
                                    $data = [ordered]@{
                                        PolicyID      = $gpo.ID
                                        Name          = $gpo.DisplayName
                                        Configuration = $config
                                        Section       = $section
                                        Element       = $Element.name
                                        Comment       = $Element.desc
                                    }
                                    New-Object -TypeName PSObject -Property $data
                                }
                            }
                            else {
                                Write-Verbose "no preference xml data found for $section"
                            }
                        }
                    }
                    else {
                        Write-Verbose "there are no preferences for this policy object"
                    }
                }
            }
            break
        }
    }
}

Export-ModuleMember -Function Get-GPOComment