Search code examples
powershellactive-directoryldap-query

Combine LDAP filters


i have 2 ldap queries and i want to combine them into one. From the first one i want to get rid of the static whenchanged with the parameters from the second one that counts 30 days in the past.

Get-ADUser -LDAPFilter '((mailNickname=id*)(whenChanged>=20170701000000.0Z))(userAccountControl=514)(|(memberof=CN=VPN1,OU=Vpn,OU=Domain Global,OU=Groups,OU=,DC=am,DC=bm,DC=cm,DC=dm)(memberof=CN=VPN2,OU=Vpn,OU=Domain Global,OU=Groups,OU=,DC=am,DC=bm,DC=cm,DC=dm))'| export-csv C:\Users\ADMIN\Desktop\Groupmembers.csv

Get-ADUser -LDAPFilter {(useraccountcontrol=514)} -Properties whenChanged | Where-Object {$_.whenChanged -gt (Get-Date).AddDays(-30)} |export-csv C:\Users\ADMID023896\Desktop\Groupmembers.csv

Thanks for any help in advance


Solution

  • $mydate = get-date ((get-date).AddDays(-30)) -format "yyyyMMdd000000.0Z"
    Get-ADUser -LDAPFilter "((mailNickname=id*)(whenchanged>=$mydate)...)"
    

    Note the change of quotes in the LDAP filter to double rather than single quotes