-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-ObjectGUID.ps1
More file actions
83 lines (67 loc) · 2.81 KB
/
Export-ObjectGUID.ps1
File metadata and controls
83 lines (67 loc) · 2.81 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
<#
.SYNOPSIS
Exports ObjectGUID and PrimarySMTPAddress from a source directory.
.DESCRIPTION
This script exports ObjectGUID and mail (SMTP address) from Active Directory objects
into a CSV file for later use.
.PARAMETER SourceDirectory
The source directory path (e.g., "OU=Users,DC=contoso,DC=com") or a searchbase for AD queries
.PARAMETER ExportPath
The file path where the CSV mapping will be saved (default: .\ObjectGUID_Mapping.csv)
.EXAMPLE
.\Export-ObjectGUID.ps1 -SourceDirectory "OU=Users,DC=contoso,DC=com"
.EXAMPLE
.\Export-ObjectGUID.ps1 -SourceDirectory "OU=Users,DC=contoso,DC=com" -ExportPath "C:\temp\mapping.csv"
#>
param(
[Parameter(Mandatory = $true)]
[string]$SourceDirectory,
[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 Export Tool"
Write-Host "================================================================================"
Write-Host ""
# ===== EXPORT PHASE =====
Write-Host "PHASE 1: EXPORT" -ForegroundColor Cyan
Write-Host "Source Directory: $SourceDirectory"
Write-Host "Export Path: $ExportPath"
Write-Host ""
try {
Write-Verbose "Retrieving AD User objects from source: $SourceDirectory"
# Retrieve User objects from AD
Write-Verbose "Querying for User objects..."
$sourceObjects = Get-ADUser -Filter * -SearchBase $SourceDirectory -Properties mail, sAMAccountName -ErrorAction SilentlyContinue |
Where-Object { $_.mail -ne $null } |
Select-Object -Property @{Name = "ObjectGUID"; Expression = { $_.ObjectGUID } },
@{Name = "PrimarySMTPAddress"; Expression = { $_.mail } },
DisplayName
if ($sourceObjects.Count -eq 0) {
Write-Warning "No objects with mail addresses found in: $SourceDirectory"
}
else {
Write-Host "Found $($sourceObjects.Count) object(s) with mail addresses" -ForegroundColor Green
}
# Export to CSV
$sourceObjects | Export-Csv -Path $ExportPath -NoTypeInformation -Encoding UTF8 -Force
Write-Host "Successfully exported mapping to: $ExportPath" -ForegroundColor Green
Write-Host ""
}
catch {
Write-Error "Error during export phase: $_"
exit 1
}
Write-Host ""
Write-Host "Export completed successfully!" -ForegroundColor Green