Searching using the script

Searching using the script

Save Below script as function.ps1

function LDAPSearch {
    param (
        [string]$LDAPQuery
    )

    # Get the PDC (Primary Domain Controller) name
    $PDC = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner.Name

    # Get the Distinguished Name of the current domain
    $DistinguishedName = ([adsi]'').distinguishedName

    # Create a new DirectoryEntry object for the LDAP search
    $DirectoryEntry = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$PDC/$DistinguishedName")

    # Create a DirectorySearcher object with the DirectoryEntry and LDAP query
    $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher($DirectoryEntry, $LDAPQuery)

    # Execute the search and return the results
    return $DirectorySearcher.FindAll()
}

Last updated