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: Get Connected Port by Process Name

# getProcessConnectionPort.ps1 $computerlist=@' SQL1 SQL2 '@ $computernames=@($computerList -split "`n")|%{$_.Trim()} $processname='sqlservr' $state='Established' $results=@() foreach ($computername in…

PowerShell: 1-Liner to Change Computer Name and Join Active Directory

# The 1-liner Add-Computer -DomainName 'somedomain.com' -credential kimconnect.com\domainadmin1 -ComputerName $env:computernname -newname 'NewName' -restart # Quick…

Microsoft SQL AlwaysOn High Availability Cluster Management Tool

There are some bugs in the script. I'm too lazy to go back a fix…