Private/Get-ADDomainControllerDirectoryEntry.ps1

Function Get-ADDomainControllerDirectoryEntry() {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)][string]$sDirectoryEntryPath,
        [Parameter(Mandatory=$false)][string]$sUserName,
        [Parameter(Mandatory=$false)][string]$sPwdClear
    )

    # This code has been placed into a separate function to enable pester testing. It is called by the function Get-ADLocalDC
    # The purpose of the function is to return the AD directory entry for the Domain Controller

    $ErrorActionPreference = "Stop"

    Write-Verbose "Function: Get-ADDomainControllerDirectoryEntry"

    if (([string]::IsNullOrEmpty($sUserName) -eq $true) -or ([string]::IsNullOrEmpty($sPwd) -eq $true)) {
        Write-Verbose "`t=== Testing without credentials"

        $objAd = New-Object System.DirectoryServices.DirectoryEntry($sDirectoryEntryPath)
    }
    else {
        Write-Verbose "`t=== Testing with credential $sUserName"

        $objAd = New-Object System.DirectoryServices.DirectoryEntry($sDirectoryEntryPath,$sUserName,$sPwdClear)

        Remove-Variable sPwdClear,sUserName -ErrorAction "SilentlyContinue"
    }
    
    return $objAd
}