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

Basic JavaScript: Understanding Uninitialized Variables

// Initialize these three variablesvar a;var b;var c;// Do not change code below this linea…

PowerShell: connectWinRM Function to Create New-PSSession

This reusable function may be included into PoSh scripts for better connection toward Windows machines…

PowerShell: Manually Create Failover Cluster SMB Shares

# Set three variables$shareName="someShare"$smbServer="someClusteredVServer"$sharePath="S:\Shares\someShare"# Generate Domain Admins label$subdomain=(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1'$domainadmins="$subdomain`\Domain…