header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
[Resolved] Search each DC one at a time using - System.DirectoryServices.DirectorySearcher
Last Post 28 Jan 2011 04:32 AM by get-james. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
get-jamesUser is Offline
New Member
New Member
Posts:59
Avatar

--
28 Jan 2011 03:10 AM
    Hi,

    I am querying AD using the following System.DirectoryServices.DirectorySearcher, but need to be able to search each of my DCs.

    $lastLogonIntervalLimit = ((date).ToUniversalTime()).AddDays(-30).ToFileTime()
    $strFilter = "(&(objectCategory=person)(objectClass=user)(lastlogon<="+$lastLogonIntervalLimit+"))" $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 100
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"
    $strProps = "samaccountname,displayname,lastlogon"
    $strProps.split(",") | %{[void]$objSearcher.PropertiesToLoad.Add($_);}
    $StrResults = $objSearcher.findall()
    $StrResults.Count

    I already have a list of my DCs using the below query, but am not sure how todo the search/query agaist one domain controller at a time.

    $StrDCs = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain() | foreach{$_.DomainControllers | foreach{$_.Name}}

    I can't use the quest commands as they are not installed on the domain and need to use native commands to Powershell 1.0 or 2.0.

    Cheers
    James
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    28 Jan 2011 04:32 AM
    Sorted it now:

    $StrDCs = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain() | foreach{$_.DomainControllers | foreach{$_.Name}}

    $lastLogonIntervalLimit = ((date).ToUniversalTime()).AddDays(-30).ToFileTime()

    $StrDCs | foreach-object {
    $Erroractionpreference = "Silentlycontinue"
    $strFilter = "(&(objectCategory=person)(objectClass=user)(lastlogon<="+$lastLogonIntervalLimit+"))"
    $StrEachDC = "LDAP://"+$_.split(".")[0
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry $StrEachDC
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 100
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"
    $strProps = "samaccountname,givenname,sn,displayname,lastlogon,pwdlastset,description,l,company,physicaldeliveryofficename,extensionAttribute4,useraccountcontrol,info,whencreated,lastlogontimestamp"
    $strProps.split(",") | %{[void]$objSearcher.PropertiesToLoad.Add($_);}
    $StrResults = $objSearcher.findall()
    $Erroractionpreference = "Continue"
    $StrAllResults += $StrResults
    }

    $StrAllResults
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer