Repository navigation
Expand file tree
/
Copy pathConnect-LdapServer.ps1
More file actions
82 lines (66 loc) · 2.86 KB
/
Copy pathConnect-LdapServer.ps1
File metadata and controls
82 lines (66 loc) · 2.86 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
<#
.SYNOPSIS
Baut eine LDAP-Verbindung auf und fuehrt eine Suche aus - ohne das
ActiveDirectory-Modul.
.DESCRIPTION
Nutzt System.DirectoryServices.Protocols direkt. Damit laesst sich auch
gegen Verzeichnisse abfragen, die kein Active Directory sind, und von
Rechnern aus, auf denen die RSAT-Werkzeuge fehlen.
Connect-LdapServer stellt die Verbindung her, Perform-LdapSearch setzt
darauf eine Suche ab.
.NOTES
Standardport 389 ist unverschluesselt. Fuer LDAPS Port 636 angeben und
die Zertifikatspruefung im Blick behalten.
#>
function Connect-LdapServer {
param (
[string]$HostName,
[string]$UserName,
[System.Security.SecureString]$Password,
[int]$Port = 389
)
$Null = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
$ldapEndpoint = if ($Port -eq 636) { "ldaps://$HostName" } else { "$HostName" }
$LDAPConnect = New-Object System.DirectoryServices.Protocols.LdapConnection $ldapEndpoint
$LDAPConnect.SessionOptions.SecureSocketLayer = ($Port -eq 636)
$LDAPConnect.SessionOptions.ProtocolVersion = 3
$LDAPConnect.AuthType = [System.DirectoryServices.Protocols.AuthType]::Basic
$credentials = New-Object System.Net.NetworkCredential -ArgumentList $UserName, $Password
Try {
$ErrorActionPreference = 'Stop'
$LDAPConnect.Bind($credentials)
$ErrorActionPreference = 'Continue'
Write-Verbose "Successfully bound to LDAP on port $Port!" -Verbose
}
Catch {
Throw "Error binding to LDAP on port $Port - $($_.Exception.Message)"
}
return $LDAPConnect
}
function Perform-LdapSearch {
param (
[System.DirectoryServices.Protocols.LdapConnection]$LDAPConnect,
[string]$BaseDn,
[string]$Filter = "(sn=Ge*)",
# [string]$Filter = "(telephoneNumber=13*)",
# [string]$Filter = "(objectClass=person)", # geht nicht, zu viele Ergebnisse
[System.DirectoryServices.Protocols.SearchScope]$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree,
[array]$Attributes = $null
)
$ModelQuery = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $BaseDn, $Filter, $Scope, $Attributes
$ModelQuery.SizeLimit = 10 # Set a size limit to avoid exceeding server constraints
Try {
$ErrorActionPreference = 'Stop'
$ModelRequest = $LDAPConnect.SendRequest($ModelQuery)
$ErrorActionPreference = 'Continue'
}
Catch {
Throw "Problem executing LDAP search - $($_.Exception.Message)"
}
return $ModelRequest
}
$hostname = Read-Host "Enter IP-Address of LDAP Server"
$username = 'uid=ldap,dc=web'
$basedn = "dc=web"
$password = Read-Host "Enter Password" -AsSecureString
$connection = Connect-LdapServer -HostName $hostname -UserName $username -Password $password -Port 389