跳到主內容

【powershell】【自訂】ldap search

function1.ps1

function LDAPSearch {
    param (
        [string]$LDAPQuery
    )

    $PDC = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner.Name
    $DistinguishedName = ([adsi]'').distinguishedName

    $DirectoryEntry = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$PDC/$DistinguishedName")

    $DirectorySearcher = New-Object System.DirectoryServices.DirectorySearcher($DirectoryEntry, $LDAPQuery)

    return $DirectorySearcher.FindAll()

}
PS C:\Users\stephanie> powershell -ep bypass
PS C:\Users\stephanie> Import-Module .\function.ps1
PS C:\Users\stephanie> $group = LDAPSearch -LDAPQuery "(&(objectCategory=group)(cn=Service Personnel*))"
PS C:\Users\stephanie> $group.properties.member
CN=Billing,CN=Users,DC=corp,DC=com
PS C:\Users\stephanie> $group = LDAPSearch -LDAPQuery "(&(objectCategory=group)(cn=Billing*))"
PS C:\Users\stephanie> $group.properties.member
CN=Customer support,CN=Users,DC=corp,DC=com
PS C:\Users\stephanie> $group = LDAPSearch -LDAPQuery "(&(objectCategory=group)(cn=Customer support*))"
PS C:\Users\stephanie> $group.properties.member
CN=michelle,CN=Users,DC=corp,DC=com
PS C:\Users\stephanie> $answer = LDAPSearch -LDAPQuery "(cn=michelle)"
PS C:\Users\stephanie> $answer.properties

Name                           Value
----                           -----
logoncount                     {0}
codepage                       {0}
objectcategory                 {CN=Person,CN=Schema,CN=Configuration,DC=corp,DC=com}
description                    {OS{xxxxx}}
usnchanged                     {536764}
instancetype                   {4}
name                           {michelle}
badpasswordtime                {0}