forked from CreativeAcer/OffboardingManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-LDAPSConnection.ps1
More file actions
165 lines (144 loc) · 6.46 KB
/
Copy pathTest-LDAPSConnection.ps1
File metadata and controls
165 lines (144 loc) · 6.46 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
function Get-DomainController {
try {
# Try method 1: Environment variable
$dc = $env:LOGONSERVER -replace '\\',''
if (-not [string]::IsNullOrEmpty($dc)) {
Write-Host "Found DC from LOGONSERVER: $dc"
return $dc
}
# Try method 2: Current domain
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$dc = $domain.DomainControllers[0].Name
if (-not [string]::IsNullOrEmpty($dc)) {
Write-Host "Found DC from current domain: $dc"
return $dc
}
# Try method 3: DNS query
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
$dc = (nslookup -type=srv _ldap._tcp.dc._msdcs.$domain 2>$null |
Select-String -Pattern "svr hostname = (.+)$" |
ForEach-Object { $_.Matches.Groups[1].Value }) | Select-Object -First 1
if (-not [string]::IsNullOrEmpty($dc)) {
Write-Host "Found DC from DNS query: $dc"
return $dc
}
throw "No Domain Controller found"
}
catch {
Write-Host "Error finding Domain Controller: $($_.Exception.Message)" -ForegroundColor Red
return $null
}
}
function Test-LDAPSConnection {
param(
[Parameter(Mandatory=$false)]
[string]$DomainController = (Get-DomainController)
)
try {
Write-Host "`n=== Testing LDAPS Configuration ==="
# Validate DC name
if ([string]::IsNullOrEmpty($DomainController)) {
throw "No Domain Controller specified"
}
Write-Host "Testing connection to Domain Controller: $DomainController"
$LDAPSPort = 636 # Define LDAPS port
# Test 1: Basic port connectivity
try {
Write-Host "`nTesting LDAPS port connectivity..."
$tcpClient = New-Object System.Net.Sockets.TcpClient
$result = $tcpClient.BeginConnect($DomainController, $LDAPSPort, $null, $null)
$waited = $result.AsyncWaitHandle.WaitOne(1000, $false)
if ($waited) {
Write-Host "✓ Port $LDAPSPort is open" -ForegroundColor Green
} else {
Write-Host "✗ Port $LDAPSPort is not accessible" -ForegroundColor Red
return
}
}
catch {
Write-Host "✗ Port connectivity test failed: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
if ($tcpClient) {
$tcpClient.Close()
}
}
# Test 2: LDAPS Binding
try {
Write-Host "`nTesting LDAPS binding..."
$ldapPath = "LDAPS://$DomainController"
$authType = [System.DirectoryServices.AuthenticationTypes]::Secure -bor
[System.DirectoryServices.AuthenticationTypes]::SecureSocketsLayer
$directoryEntry = New-Object System.DirectoryServices.DirectoryEntry($ldapPath, $null, $null, $authType)
# Try to access a property to verify connection
$name = $directoryEntry.Name
Write-Host "✓ LDAPS binding successful" -ForegroundColor Green
Write-Host "`nLDAPS connection details:"
Write-Host " Server: $DomainController"
Write-Host " Path: $($directoryEntry.Path)"
Write-Host " Name: $name"
# Test 3: SSL Certificate (only if binding successful)
try {
Write-Host "`nTesting SSL Certificate..."
$tcpClient = New-Object System.Net.Sockets.TcpClient($DomainController, $LDAPSPort)
$sslStream = New-Object System.Net.Security.SslStream(
$tcpClient.GetStream(),
$false,
{ param($sender, $certificate, $chain, $errors) return $true }
)
$sslStream.AuthenticateAsClient($DomainController)
$cert = $sslStream.RemoteCertificate
if ($cert -ne $null) {
# Convert to X509Certificate2 for better property access
$cert2 = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($cert)
Write-Host "Certificate details:" -ForegroundColor Cyan
Write-Host " Subject: $($cert2.Subject)"
Write-Host " Issuer: $($cert2.Issuer)"
Write-Host " Valid From: $($cert2.NotBefore.ToString('yyyy-MM-dd HH:mm:ss'))"
Write-Host " Valid To: $($cert2.NotAfter.ToString('yyyy-MM-dd HH:mm:ss'))"
Write-Host " Thumbprint: $($cert2.Thumbprint)"
if ($cert2.NotAfter -gt (Get-Date)) {
Write-Host "✓ Certificate is valid" -ForegroundColor Green
} else {
Write-Host "✗ Certificate has expired" -ForegroundColor Red
}
} else {
Write-Host "✗ SSL Certificate is null or could not be retrieved" -ForegroundColor Red
}
} catch {
Write-Host "✗ SSL Certificate validation failed: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
if ($sslStream -ne $null) {
try { $sslStream.Dispose() } catch { Write-Host "Error disposing SslStream: $($_.Exception.Message)" }
}
if ($tcpClient -ne $null) {
try { $tcpClient.Dispose() } catch { Write-Host "Error disposing TcpClient: $($_.Exception.Message)" }
}
}
}
catch {
Write-Host "✗ LDAPS binding failed: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
if ($directoryEntry) {
$directoryEntry.Dispose()
}
}
}
catch {
Write-Host "`n✗ Test failed: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
Write-Host "`n=== Test Complete ===`n"
}
}
# Call the function with the automatically detected DC
$dc = $env:LOGONSERVER -replace '\\',''
if ([string]::IsNullOrEmpty($dc)) {
Write-Host "Could not detect Domain Controller from environment." -ForegroundColor Yellow
Write-Host "Please provide a Domain Controller name." -ForegroundColor Yellow
} else {
Write-Host "Testing LDAPS for Domain Controller: $dc"
Test-LDAPSConnection -DomainController $dc
}