Author Posts

August 8, 2014 at 1:13 am

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

August 8, 2014 at 2:37 am

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

August 8, 2014 at 2:53 am

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.

August 8, 2014 at 2:56 am

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

August 8, 2014 at 2:59 am

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

August 8, 2014 at 3:10 am

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 *

August 8, 2014 at 3:17 am

The latter. Simple as that. Thank you

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

August 8, 2014 at 3:33 am

Ah easy – totally missed that cmdlet get-addomaincontroller

August 8, 2014 at 4:39 am

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