Set-LDAPSBinding.ps1


Description

Purpose

No synopsis provided.

Detailed Description

No detailed description provided.

Back to Top

Usage

No usage examples provided.

Back to Top

Notes

No additional notes.

Back to Top


Script

function Set-LDAPSBinding {
    param (
        [Parameter(Mandatory=$true)]
        [string]$DomainControllerFQDN,
        
        [Parameter(Mandatory=$true)]
        [string]$CertThumbprint,
        
        [Parameter(Mandatory=$true)]
        [pscredential]$Credential
    )

    $scriptBlock = {
        param (
            [string]$DomainControllerFQDN,
            [string]$CertThumbprint
        )

        Write-Verbose "Searching for certificate with thumbprint $CertThumbprint on $DomainControllerFQDN"
        
        $cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {
            $_.Thumbprint -eq $CertThumbprint
        }

        if ($null -ne $cert) {
            Write-Verbose "Certificate found. Binding certificate with thumbprint $CertThumbprint to LDAP service on $DomainControllerFQDN"

            # Bind the certificate to the NTDS service
            New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Parameters" -Name "ClientLDAPSAuthentication" -Value 1 -PropertyType DWORD -Force | Out-Null
            New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Parameters" -Name "ClientLDAPS" -Value 1 -PropertyType DWORD -Force | Out-Null

            # Bind the certificate to the LDAP service
            $store = [System.Security.Cryptography.X509Certificates.X509Store]::new("NTDS", "LocalMachine")
            $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
            $store.Add($cert)
            $store.Close()

            Write-Verbose "Certificate with thumbprint $CertThumbprint successfully bound to LDAP service on $DomainControllerFQDN"
        } else {
            Write-Warning "No valid certificate found with thumbprint $CertThumbprint for LDAPS on $DomainControllerFQDN"
        }
    }

    Invoke-Command -ComputerName $DomainControllerFQDN -ScriptBlock $scriptBlock -ArgumentList $DomainControllerFQDN, $CertThumbprint -Credential $Credential -Verbose
}

# Example usage of the function:
# Set-LDAPSBinding -DomainControllerFQDN "RDGDC01.rdg.co.uk" -CertThumbprint "9f580f463113ea7615847821ad3775d690c640d2" -Credential (Get-Credential) -Verbose

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top