Jump to content

Querying Nearest Domain Controller In Winpe


Recommended Posts

Posted

I have a LDAP query that I use to query AD. I query AD for a specific useraccoutnname in my Win PE environment. I query AD before even the machine gets ghosted and way before even I log into any domain.

I need to use the name of my DC in my LDAP query that queries AD because without that my query fails. Currently I have been harcoding the name of the Domain controller, but can you get the name of the nearest DC in WinPE environment before logging to any domain.

This is what I had tried, but did not work for me.

Set MyDomain = GetObject("LDAP://RootDSE")

MyDC = MyDomain.Get("dnsHostName")

The above query basically gives me the name of the DC.

I then tried to use this MyDC variable in my LDAP search but this did not work in WinPE (works fine from my machine as I am already logged on the domain).

Any thoughts or suggestions of getting the name of Domain Controller.


Posted

Got it. Hope it helps someone.

Call this API and it might just work.

Option Explicit

MsgBox GetPDCName("computername", "domainnname")

'The NetGetDCName function returns the name of the Primary Domain Controller (PDC) for the specified domain.

'API calls

Private Declare Function NetGetDCName Lib "netapi32.dll" (ServerName As Any, DomainName As Any, lpBuffer As Long) As Long

Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long

Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)

Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long

'API Constants

Private Const NERR_Success As Long = 0&

'Module

Private Function PtrToString(lpwString As Long) As String

'Convert a LPWSTR pointer to a VB string

Dim Buffer() As Byte

Dim nLen As Long

If lpwString Then

nLen = lstrlenW(lpwString) * 2

If nLen Then

ReDim Buffer(0 To (nLen - 1)) As Byte

CopyMem Buffer(0), ByVal lpwString, nLen

PtrToString = Buffer

End If

End If

End Function

'ComputerName

'Pointer to string containing the name of the remote server on which the function is to execute. A NULL string specifies the local computer.

'DomainnNme

'Pointer to a string containing the name of the domain. A NULL string indicates that the function returns the name of the domain controller for the primary domain.

Public Function GetPDCName(ComputerName As String, DomainName As String) As String

Dim bComputer() As Byte

Dim bDomain() As Byte

Dim ret As Long

Dim lpBuffer As Long

Dim s As String

If Trim(ComputerName) = "" Then

'Local users

bComputer = vbNullChar

Else

'Check the syntax of the ServerName string

If InStr(ComputerName, "\\") = 1 Then

bComputer = ComputerName & vbNullChar

Else

bComputer = "\\" & ComputerName & vbNullChar

End If

End If

If Trim(DomainName) = "" Then

'Default Domain

bDomain = vbNullChar

Else

bDomain = DomainName & vbNullChar

End If

ret = NetGetDCName(bComputer(0), bDomain(0), lpBuffer)

If ret = NERR_Success And lpBuffer Then

s = PtrToString(lpBuffer)

End If

If lpBuffer Then

Call NetApiBufferFree(lpBuffer)

End If

GetPDCName = s

End Function

'Usage

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...