Trying to pick from list of DCs

Welcome Forums General PowerShell Q&A Trying to pick from list of DCs

This topic contains 8 replies, has 3 voices, and was last updated by

 
Participant
4 years, 4 months ago.

  • Author
    Posts
  • #17883

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

  • #17891

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    The latter. Simple as that. Thank you

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

  • #17895

    Participant
    Points: 0
    Rank: Member

    Ah easy – totally missed that cmdlet get-addomaincontroller

  • #17898

    Participant
    Points: 21
    Rank: Member

    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

The topic ‘Trying to pick from list of DCs’ is closed to new replies.