Get-LogonServer Function

I have noticed in my testing that the %LOGONSERVER% environmental variable can sometimes be unreliable. After searching some time on Google, I found that using the nltest /dsgetdc: command was the most reliable method of returning the logon server. This unfortunately provides some complications since the results of the command also gives additional information that is not needed if you just need the current logon server. This is why I created the following Get-LogonServer function so you could easily return the logon server and use it in a Powershell script.

Function Get-LogonServer {

    Try {

        (nltest /dsgetdc: | findstr 'DC: ').split(" ") | %{
 
	        If($_ -like '\\*'){

			    $DC= $_.replace('\\','')

	        }

        }

    }
    Catch {
        
	    $DC = $False

    }
 
 
    If ($DC){

        $DC

    }
    Else {

        Return $False

    }
  
}

1 Comment

  1. Thanks for saving the day!
    Brilliant script.

Leave a Reply