Public/Get-ADDomainControllerRootEntry.ps1

Function Get-ADDomainControllerRootEntry() {
    <#
        .SYNOPSIS
        This function uses a .net call to attempt to return the RootDSE entry from a Domain Controller.
 
        .DESCRIPTION
 
        .PARAMETER sDCName
        [Mandatory] This string parameter should specify the Domain Controller to contact
 
        .EXAMPLE
        $objAD = Get-ADDomainControllerRootEntry -sDCName "myDC.mydomain.com"
 
        The returned object will contain the RootDSE reply. If this object contains the property defaultNamingContext then
        the DC is live and is available for use.
 
        .NOTES
        MVogwell
 
        Version history:
            1.0 - Initial release
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)][string]$sDCName
    )

    BEGIN {
        $ErrorActionPreference = "Stop"
    }
    PROCESS {
        try {
            # Build the LDAP RootDSE path containing the DC name to test
            $sDirectoryEntryPath = "LDAP://" + $sDCName + "/RootDSE"
            $objAd = New-Object System.DirectoryServices.DirectoryEntry($sDirectoryEntryPath)
        }
        catch {
            # If the call fails against the DC then return null
            $objAd = $null
        }
    }
    END {
        return Write-Output $objAd -NoEnumerate
    }
}