powershelldomaincontroller

Powershell - DC Locator Script to retrieve available domain Controller


I am integrating Active Directory connector with a tool where I must enter below format of URI to connect :

ldaps://domainController1.domain.testdomain.local:636/

If the given domain controller is not active, a DC locator script is available to configure a high-availability connection to Active Directory. This script should returns with the available Active Domain Controller list so it can be used by the AD connector.

By looking at some documentation, I wrote the below script

Script=(get-addomaincontroller -DomainName 'domain.testdomain.local' -Discover).hostname

This script returns the below map of available domain controller :

[{"Objectreturned":"availableDC.domain.testdomain.local","Length":"24"}]

Unfortunately, even if the script retrieves correctly an active domain controller, I am not able to connect since I think the tool except the first given format.

Is there any other PS script that could format the available DC as shown URI ?

Thanks !


Solution

  • Sounds like you just need a bit of string formatting:

    $domainName = 'domain.testdomain.local'
    $DC = Get-ADDomainController -DomainName $domainName -Discover 
    
    return 'ldaps://{0}:636/' -f $DC.Hostname[0]