functions/Import-GptLink.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
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
203
204
205
206
207
function Import-GptLink
{
<#
    .SYNOPSIS
        Imports GPO Links.
     
    .DESCRIPTION
        Imports GPO Links.
        Use this to restore the exported links in their original order (or as close to it as possible).
     
    .PARAMETER Path
        The path from which to pick up the import file.
     
    .PARAMETER Name
        Only restore links of matching GPOs
     
    .PARAMETER Domain
        The domain into which to import.
     
    .EXAMPLE
        PS C:\> Import-GptLink -Path '.'
     
        Import GPO Links based on the exported links stored in the current path.
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $Path,
        
        [string[]]
        $Name = '*',
        
        [string]
        $Domain = $env:USERDNSDOMAIN
    )
    
    begin
    {
        #region Utility Functions
        function Get-OU
        {
        <#
            .SYNOPSIS
                Retrieves an OU. Caches results.
             
            .DESCRIPTION
                Retrieves an OU. Caches results.
                Results are cached separately for each domain/server.
             
            .PARAMETER DistinguishedName
                The name of the OU to check.
             
            .PARAMETER Server
                The domain or server to check against.
             
            .EXAMPLE
                PS C:\> Get-OU -DistinguishedName $dn -Server $Domain
             
                Return the OU pointed at with $dn if it exists.
        #>

            [CmdletBinding()]
            param (
                [Parameter(Mandatory = $true)]
                [string]
                $DistinguishedName,
                
                [Parameter(Mandatory = $true)]
                [string]
                $Server
            )
            
            if (-not $script:targetOUs) { $script:targetOUs = @{ } }
            if (-not $script:targetOUs[$Server]) { $script:targetOUs[$Server] = @{ } }
            
            if ($script:targetOUs[$Server].ContainsKey($DistinguishedName))
            {
                return $script:targetOUs[$Server][$DistinguishedName]
            }
            
            try
            {
                $paramGetADOrganizationalUnit = @{
                    Identity    = $DistinguishedName
                    Server        = $Server
                    Properties  = 'gpLink'
                    ErrorAction = 'Stop'
                }
                $script:targetOUs[$Server][$DistinguishedName] = Get-ADOrganizationalUnit @paramGetADOrganizationalUnit
            }
            catch { $script:targetOUs[$Server][$DistinguishedName] = $null }
            return $script:targetOUs[$Server][$DistinguishedName]
        }
        
        function Set-GPLinkSet
        {
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
            [CmdletBinding()]
            param (
                $LinkObject,
                
                $Domain,
                
                $AllGpos,
                
                $Server
            )
            
            foreach ($linkItem in $LinkObject)
            {
                $linkItem.Index = [int]($linkItem.Index)
                $linkItem.TotalCount = [int]($linkItem.TotalCount)
            }
            $orgUnit = Get-OU -DistinguishedName $LinkObject[0].TargetOU -Server $Domain
            $insertIndex = 1
            foreach ($linkItem in ($LinkObject | Sort-Object Index))
            {
                if ($orgUnit.LinkedGroupPolicyObjects -contains $linkItem.Policy.CleanedPath)
                {
                    $insertIndex = $orgUnit.LinkedGroupPolicyObjects.IndexOf($linkItem.Policy.CleanedPath) + 1
                    continue
                }
                
                $paramSetGPLink = @{
                    LinkEnabled = 'Yes'
                    Guid        = $linkItem.Policy.ID
                    Order        = $insertIndex
                    Domain        = $Domain
                    Enforced    = 'No'
                    Target        = $orgUnit
                    Server        = $Server
                    ErrorAction = 'Stop'
                }
                if ($linkItem.State -eq "1") { $paramSetGPLink['LinkEnabled'] = 'No' }
                if ($linkItem.State -eq "2") { $paramSetGPLink['Enforced'] = 'Yes' }
                
                try
                {
                    $null = New-GPLink @paramSetGPLink
                    New-ImportResult -Action 'Importing Group Policy Links' -Step 'Applying Link' -Target $linkItem.GpoName -Data $linkItem -Success $true
                }
                catch
                {
                    if ($_.Exception.InnerException.HResult -eq 0x800700B7)
                    {
                        New-ImportResult -Action 'Importing Group Policy Links' -Step 'Applying Link: Already Exists' -Target $linkItem.GpoName -Data $linkItem -Success $true -ErrorData $_
                    }
                    else
                    {
                        New-ImportResult -Action 'Importing Group Policy Links' -Step 'Applying Link' -Target $linkItem.GpoName -Data $linkItem -Success $false -ErrorData $_
                    }
                }
                
                $insertIndex++
            }
        }
        #endregion Utility Functions
        
        $PSDefaultParameterValues['New-ImportResult:Action'] = 'Importing Group Policy Links'
        $PSDefaultParameterValues['New-ImportResult:Success'] = $false
        
        $pathItem = Get-Item -Path $Path
        if ($pathItem.Extension -eq '.csv') { $resolvedPath = $pathItem.FullName }
        else { $resolvedPath = (Get-ChildItem -Path $pathItem.FullName -Filter 'gp_links_*.csv' | Select-Object -First 1).FullName }
        if (-not $resolvedPath) { throw "Could not find GPO Links file in $($pathItem.FullName)" }
        
        $domainObject = Get-ADDomain -Server $Domain
        $policyObjects = Get-GPO -All -Domain $Domain | Select-Object *, @{
            Name       = 'CleanedPath'
            Expression = { $_.Path -replace $_.ID, $_.ID }
        }
        $linkData = Import-Csv $resolvedPath | Where-Object {
            Test-Overlap -ReferenceObject $_.GpoName -DifferenceObject $Name -Operator Like
        } | Select-Object *, @{
            Name            = "Policy"
            Expression        = {
                $linkItem = $_
                $policyObjects | Where-Object DisplayName -EQ $linkItem.GpoName
            }
        }, @{
            Name                                                                       = "TargetOU"
            Expression                                                                   = {
                '{0},{1}' -f ($_.OUDN -replace ',DC=\w+'), $domainObject.DistinguishedName
            }
        }
    }
    process
    {
        $groupedLinks = $linkData | Group-Object -Property GpoName
        $groupedLinks | Where-Object Name -NotIn $policyObjects.DisplayName | ForEach-Object {
            New-ImportResult -Step 'Checking GPO existence' -Target $_.Name -Data $_.Group -ErrorData "GPO $($_.Name) does not exist"
        }
        $linksPolicyExists = ($groupedLinks | Where-Object Name -In $policyObjects.DisplayName).Group
        $linksPolicyExists | Where-Object { -not (Get-OU -DistinguishedName $_.TargetOU -Server $Domain) } | ForEach-Object {
            New-ImportResult -Step 'Checking OU existence' -Target $_.GpoName -Data $_ -ErrorData "OU $($_.TargetOU) does not exist, cannot link $($_.GpoName)"
        }
        $linksToProcess = $linksPolicyExists | Where-Object { Get-OU -DistinguishedName $_.TargetOU -Server $Domain }
        
        $groupedToProcess = $linksToProcess | Group-Object -Property TargetOU
        foreach ($linkSet in $groupedToProcess)
        {
            Set-GPLinkSet -LinkObject $linkSet.Group -Domain $domainObject.DNSRoot -AllGpos $policyObjects -Server $domainObject.PDCEmulator
        }
    }
}