Trying to pick from list of DCs

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

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

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.

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

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

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 *

The latter. Simple as that. Thank you

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

Ah easy - totally missed that cmdlet get-addomaincontroller

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