PoshCode Archive  Artifact [a7823370a4]

Artifact a7823370a4e4573031019dd59255451abe41bf384e14458499c05c82cdb79d91:

  • File Get-OpenLDAP.ps1 — part of check-in [8e0b0f6b73] at 2018-06-10 13:48:54 on branch trunk — This script performs OpenLdap query against specified Server. (user: BSonPosh size: 1571)

# encoding: ascii
# api: powershell
# title: Get-OpenLDAP.ps1
# description: This script performs OpenLdap query against specified Server.
# version: 0.1
# type: class
# author: BSonPosh
# license: CC0
# x-poshcode-id: 5025
# x-derived-from-id: 5026
# x-archived: 2016-05-24T22:21:21
# x-published: 2016-03-28T03:40:00
#
# Note: $Path is the LDAP path for the User account to authenticate with.
#
Param($user,
      $password = $(Read-Host "Enter Password" -asSec),
      $filter = "(objectclass=user)",
      $server = $(throw '$server is required'),
      $path = $(throw '$path is required'),
      [switch]$all,
      [switch]$verbose)
    
function GetSecurePass ($SecurePassword) {
  $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecurePassword)
  $password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
  [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
  $password
}    

if($verbose){$verbosepreference = "Continue"}

$DN = "LDAP://$server/$path"
Write-Verbose "DN = $DN"
$auth = [System.DirectoryServices.AuthenticationTypes]::FastBind
Write-Verbose "Auth = FastBind"
$de = New-Object System.DirectoryServices.DirectoryEntry($DN,$user,(GetSecurePass $Password),$auth)
Write-Verbose $de
Write-Verbose "Filter: $filter"
$ds = New-Object system.DirectoryServices.DirectorySearcher($de,$filter) 
Write-Verbose $ds
if($all)
{
    Write-Verbose "Finding All"
    $ds.FindAll()
}
else
{
    Write-Verbose "Finding One"
    $ds.FindOne()
}