Posted On November 18, 2020

PowerShell: Test LDAPS Connection

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Test LDAPS Connection
Function testLdap {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0,ValueFromPipeline=$true)]$dcs=$($ENV:LOGONSERVER -replace '\\',''),
        [Parameter(Position=1,ValueFromPipeline=$true)]$port='636'
    ) 

    $ErrorActionPreference = "Stop"
    $results = @()
    try{
        Import-Module ActiveDirectory
    }catch{
        write-warning "Active Directory module is not available on $env:computername."
        break
    }
    ForEach($dc in $dcs){
        Try{
            $validatedDc = (Get-ADDomainController -Identity $dc).hostname
        }Catch{
            write-warning $_
            Continue
        }

        If($Null -ne $validatedDc){  
            Try{
                $ldaps = [adsi]"LDAP://$($validatedDc):$port"
                $ldapPath=$ldaps.Path
                If ($ldapPath){
                    $result=New-Object PSObject -Property ([ordered]@{ 
                        DC=$dc
                        Port=$port
                        Path=$ldapPath
                        Success=$true
                    })
                    $results+=$result
                }
            }Catch{
                write-warning $_
                continue
            }        
        }
    }

    If($results){
        return $Results
    }else{
        write-host "No LDAP Connection success for $dcs"
    }
}

testLdap

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell Script to Copy Members of One Group to Another

#Author: Kim Doan#Please tell me this code suck so that I can improve # Adding…

PowerShell: check whether the current user is a member of Domain Admins

# short snippet to check whether the currently login user is a domain admin$CurrentUser =…

Basic CSS: Add Rounded Corners with border-radius

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"><style>.red-text {color: red;}h2 {font-family: Lobster, monospace;}p {font-size: 16px;font-family: monospace;}.thick-green-border {border-color: green;border-width: 10px;border-style:…