-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport-ObjectGUID2ConsistencyGUID.ps1
More file actions
223 lines (186 loc) · 9.05 KB
/
Import-ObjectGUID2ConsistencyGUID.ps1
File metadata and controls
223 lines (186 loc) · 9.05 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<#
.SYNOPSIS
Imports ObjectGUID mappings into a destination directory, setting msDSConsistencyGUID.
.DESCRIPTION
This script imports ObjectGUID mappings from a CSV file and matches objects in
the destination directory by PrimarySMTPAddress, then sets their msDSConsistencyGUID
attribute to the source ObjectGUID
.PARAMETER DestinationDirectory
The destination directory path or identifier where objects will be matched and updated
.PARAMETER ExportPath
The file path to the CSV mapping file to import (default: .\ObjectGUID_Mapping.csv)
.EXAMPLE
.\Import-ObjectGUID2ConsistencyGUID.ps1 -DestinationDirectory "DC=contoso,DC=com"
.EXAMPLE
.\Import-ObjectGUID2ConsistencyGUID.ps1 -DestinationDirectory "DC=contoso,DC=com" -ExportPath "C:\temp\mapping.csv"
#>
param(
[Parameter(Mandatory = $true)]
[string]$DestinationDirectory,
[Parameter(Mandatory = $false)]
[string]$ExportPath = ".\ObjectGUID_Mapping.csv"
)
# ===== IMPORT REQUIRED MODULE =====
try {
Import-Module ActiveDirectory -ErrorAction Stop
}
catch {
Write-Error "Failed to import ActiveDirectory module. Please ensure the AD PowerShell module is installed."
exit 1
}
# ===== CONFIGURATION =====
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
Write-Host "================================================================================"
Write-Host "ObjectGUID & PrimarySMTPAddress Import Tool"
Write-Host "================================================================================"
Write-Host ""
# ===== IMPORT PHASE =====
if (-not (Test-Path $ExportPath)) {
Write-Error "Import file not found at: $ExportPath"
exit 1
}
Write-Host "PHASE 1: IMPORT & MATCH" -ForegroundColor Cyan
Write-Host "Destination Directory: $DestinationDirectory"
Write-Host "Source CSV: $ExportPath"
Write-Host ""
try {
# Import the mapping
Write-Verbose "Importing ObjectGUID mapping from CSV..."
$mappingData = Import-Csv -Path $ExportPath -Encoding UTF8
if ($null -eq $mappingData) {
Write-Error "No data found in CSV file"
exit 1
}
# Normalize to array (single object imports as object, not array)
if ($mappingData -is [object] -and $mappingData -isnot [array]) {
$mappingData = @($mappingData)
}
Write-Host "Loaded $($mappingData.Count) mapping record(s)" -ForegroundColor Green
Write-Host ""
# Match and process objects in destination
Write-Verbose "Retrieving AD User objects from destination: $DestinationDirectory..."
$destObjects = Get-ADUser -Filter * -SearchBase $DestinationDirectory -Properties mail, sAMAccountName, "ms-DS-ConsistencyGUID" -ErrorAction SilentlyContinue
Write-Host "Found $($destObjects.Count) object(s) in destination directory" -ForegroundColor Green
Write-Host ""
# Matching logic
$matchedCount = 0
$unmatchedCount = 0
$updatedCount = 0
$updateErrorCount = 0
$results = @()
foreach ($mapping in $mappingData) {
$primarySmtp = $mapping.PrimarySMTPAddress
$originalGuid = $mapping.ObjectGUID
Write-Verbose "Processing: $primarySmtp"
# Find matching object in destination by PrimarySMTPAddress
$destObject = $destObjects | Where-Object { $_.mail -eq $primarySmtp }
if ($destObject) {
$matchedCount++
$msDSConsistencyGUIDStatus = "PENDING"
$msDSConsistencyGUIDError = $null
# Attempt to set msDSConsistencyGUID attribute
try {
Write-Verbose "Attempting to set msDSConsistencyGUID for $primarySmtp"
# Convert the source GUID to binary format for msDSConsistencyGUID
if ($originalGuid -is [guid]) {
$guidBytes = $originalGuid.ToByteArray()
}
else {
$guidBytes = [guid]::Parse($originalGuid).ToByteArray()
}
# Check if ms-DS-ConsistencyGUID already has a value
$currentValue = $destObject."ms-DS-ConsistencyGUID"
if ($null -ne $currentValue -and $currentValue -ne $guidBytes) {
# Convert current value to compare
$currentGuid = [guid]$currentValue
$incomingGuid = [guid]$guidBytes
if ($currentGuid -ne $incomingGuid) {
$msDSConsistencyGUIDStatus = "FAILED"
$msDSConsistencyGUIDError = "Attribute already contains a different value: $currentGuid. Will not overwrite with $incomingGuid"
$updateErrorCount++
Write-Host "CONFLICT: $primarySmtp - Attribute already has value $currentGuid, not overwriting with $incomingGuid" -ForegroundColor Yellow
}
else {
# Values are the same, mark as success
$msDSConsistencyGUIDStatus = "SUCCESS"
$updatedCount++
Write-Host "MATCHED: $primarySmtp -> msDSConsistencyGUID already set correctly" -ForegroundColor Green
}
}
else {
# Attribute is empty or doesn't exist, proceed with update
Set-ADObject -Identity $destObject.DistinguishedName -Replace @{ "ms-DS-ConsistencyGUID" = $guidBytes } -ErrorAction Stop
$msDSConsistencyGUIDStatus = "SUCCESS"
$updatedCount++
Write-Host "MATCHED AND UPDATED: $primarySmtp -> msDSConsistencyGUID set" -ForegroundColor Green
}
}
catch {
$msDSConsistencyGUIDStatus = "FAILED"
$msDSConsistencyGUIDError = $_.Exception.Message
$updateErrorCount++
Write-Host "MATCHED BUT UPDATE FAILED: $primarySmtp - $_" -ForegroundColor Yellow
}
$result = [PSCustomObject]@{
PrimarySMTPAddress = $primarySmtp
SourceObjectGUID = $originalGuid
DestinationObjectGUID = $destObject.ObjectGUID
DestinationDisplayName = $destObject.DisplayName
msDSConsistencyGUIDStatus = $msDSConsistencyGUIDStatus
msDSConsistencyGUIDError = $msDSConsistencyGUIDError
Status = "MATCHED"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
else {
$unmatchedCount++
$result = [PSCustomObject]@{
PrimarySMTPAddress = $primarySmtp
SourceObjectGUID = $originalGuid
DestinationObjectGUID = $null
DestinationDisplayName = $null
msDSConsistencyGUIDStatus = "N/A"
msDSConsistencyGUIDError = $null
Status = "NOT_FOUND"
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
Write-Host "NOT FOUND: $primarySmtp" -ForegroundColor Yellow
}
$results += $result
}
Write-Host ""
Write-Host "=" * 80
Write-Host "IMPORT RESULTS" -ForegroundColor Cyan
Write-Host "=" * 80
Write-Host "Matched: $matchedCount"
Write-Host "Successfully Updated: $updatedCount"
Write-Host "Update Errors: $updateErrorCount"
Write-Host "Not Found: $unmatchedCount"
Write-Host "Total: $($results.Count)"
Write-Host ""
# Export matching results
$resultsPath = ($ExportPath -replace '\.csv$', '') + "_Results_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
$results | Export-Csv -Path $resultsPath -NoTypeInformation -Encoding UTF8 -Force
Write-Host "Results exported to: $resultsPath" -ForegroundColor Green
# Summary table
Write-Host ""
Write-Host "MATCHED OBJECTS:" -ForegroundColor Green
$results | Where-Object { $_.Status -eq "MATCHED" } | Format-Table -Property PrimarySMTPAddress, SourceObjectGUID, DestinationObjectGUID, msDSConsistencyGUIDStatus -AutoSize
if ($updateErrorCount -gt 0) {
Write-Host ""
Write-Host "UPDATE ERRORS:" -ForegroundColor Yellow
$results | Where-Object { $_.msDSConsistencyGUIDStatus -eq "FAILED" } | Format-Table -Property PrimarySMTPAddress, msDSConsistencyGUIDError -AutoSize
}
if ($unmatchedCount -gt 0) {
Write-Host ""
Write-Host "UNMATCHED OBJECTS:" -ForegroundColor Yellow
$results | Where-Object { $_.Status -eq "NOT_FOUND" } | Format-Table -Property PrimarySMTPAddress, SourceObjectGUID -AutoSize
}
}
catch {
Write-Error "Error during import phase: $_"
exit 1
}
Write-Host ""
Write-Host "Script completed successfully!" -ForegroundColor Green