Trying to pick from list of DCs

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Martin9700 Martin9700 2 years, 3 months ago.

  • Author
    Posts
  • #17883
    Profile photo of lemonade
    lemonade
    Participant

    Just looking for an easy way to pick from a list of our domain controllers. Managed to get so far, but the output shows in 1 column. It pulls the name and site info in one long list rather than site next to name in 2 columns.

    Wondered if it was possible to have it in 2 columns. Also tried with listview form rather than out-gridview, but I'm getting the same issue.

    function changedc {
    $Config = ([adsi]"LDAP://rootdse").ConfigurationNamingContext
    $dse = [adsi]"LDAP://$config"
    $searcher = New-Object DirectoryServices.DirectorySearcher ($dse, "ObjectClass=nTDSDSA")
    $searcher.findall() | ForEach-Object{ ([adsi]$_.path).parent } | foreach { ($_ -split 'CN=')[1] -replace ','}
    $Config = ([adsi]"LDAP://rootdse").ConfigurationNamingContext
    $dse = [adsi]"LDAP://$config"
    $searcher = New-Object DirectoryServices.DirectorySearcher ($dse, "ObjectClass=nTDSDSA")
    $searcher.findall() | ForEach-Object{ ([adsi]$_.path).parent } | foreach { ($_ -split 'CN=')[3] -replace ','}
    }
    changedc | Out-GridView
    

    Thanks

  • #17884
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    does something like this work for you? its not using the LDAP but the activedirectory module instead

    
    
    $domain = get-addomain
    $domaincontrollers = $domain.ReplicaDirectoryServers
    $forest = Get-ADForest
    
    
    $domaindetail = foreach ($domaincontroller in $domaincontrollers) {
        $props = @{ 
    
        'Domain Controller' = $domaincontroller
        'Sites' = $forest.Sites
        }
    
    New-Object -TypeName PSObject -Property $props
    }
    
    $domaindetail | Out-GridView
    
    
  • #17887
    Profile photo of lemonade
    lemonade
    Participant

    Thanks for that. Its pulling the sites information, but not the site info for each specific DC. Trying to get name dc1 site office1, dc2 office2 etc. So when a user

    I think the issue is site isn't an attribute. Not sure though.

  • #17889
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    oh yes i see.

    Try this – problem is im testing where i only have a single site thats why bit tricky to get you exactly what you need. This might work

    
    $domain = get-addomain
    $domaincontrollers = $domain.ReplicaDirectoryServers
    $forest = Get-ADForest
    
    
    $domaindetail = foreach ($domaincontroller in $domaincontrollers) {
        $props = @{ 
    
        'Domain Controller' = $forest.DomainNamingMaster 
        'Sites' = $forest.Sites.Value
        }
    
    New-Object -TypeName PSObject -Property $props
    }
    
    $domaindetail | Out-GridView
    
    
  • #17890
    Profile photo of lemonade
    lemonade
    Participant

    Still same. Comes out as {Default-First-Site-Name,DC2,DC3,etc,etc}

  • #17891
    Profile photo of Martin9700
    Martin9700
    Participant

    Get-ADDomainController will get all of the information you need, including Site. Are you purposefully staying away from the AD tools or just haven't gone there yet?

    Get-ADDomainController -Filter *
  • #17893
    Profile photo of lemonade
    lemonade
    Participant

    The latter. Simple as that. Thank you

    Get-ADDomainController -Filter * | Select-Object name,site | out-gridview -title "Change Domain Controller" -PassThru

  • #17895
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    Ah easy – totally missed that cmdlet get-addomaincontroller

  • #17898
    Profile photo of Martin9700
    Martin9700
    Participant

    There are some fun parameters with Get-ADDomainController too. If you're looking for the closest domain controller to the computer you're running the script from:

    Get-ADDomainController -Discover -Domain yourdomain.com

You must be logged in to reply to this topic.