AD Bind Test Try Catch

Hello Team,

I need to test the AD Bind with port 636 for successful server names & failed servers name . How to handle this ? Catch is not working.

[pre]

$Servers = ([system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainControllers).Name
$ErrorActionPreference = “Stop”
$Invoke_Bind = foreach ($Server in $Servers)
{
Try
{
$ldaps = [adsi]“LDAP://$($Server):636”
[pscustomobject]@{
DC = $Server
Path = $ldaps.path

}
}
Catch
{
$Error[0].Exception.InnerException
}

}

[/pre]

 

You have to manually examine the results. So, this one is maybe a better fit for a simple if/then. So, this…

$Servers = ([system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainControllers).Name,'dc02'
($Invoke_Bind = foreach ($Server in $Servers)
{

    $ldaps = [adsi]"LDAP://$($Server):636"

        [pscustomobject]@{
        DC = $Server
        Path = $ldaps.path
    }

    If ($ldaps.path -eq $null)
    {Write-Warning -Message "$Server not valid"}
    Else {'success'}

})

# Results

WARNING: dc02 not valid

DC                     Path
--                     ----
dc01.contoso.com       LDAP://dc01.contoso.com:636
success
dc02 

Of course if you a bent on using Try/Catch, you just put that in there. As shown here:

http://www.virtu-al.net/2013/01/17/checking-ad-for-secure-ldap-connections-with-powershell

… but why over complicate this.

Or just use scripts samples available and tweak as needed. For example:

Function Test-LDAPConnection