How a computer in a Workgroup can detect if a Domain Controller exists ?

Peter Rietmann 106 Reputation points
2022-05-05T20:11:31.767+00:00

I have a script that will run on multiple Servers that start out as Windows Server 2016 in a workgroup

If a Domain controller doesn't exists (ABC.COM) Then Promote the Server to Primary Domain Controller creating ABC.COM
If a Domain controller does exist then Promote the Server to Domain Controller joining ABC.COM

If a server is in a workgroup the command Get-ADDomain -Identity "DC=ABC,dc=com" does not return the PDC if it exists.

So what would my powershell function have to be ?
DoesPDCExist
{

return true if PDC exists

return false if PDS does not exist

}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Philippe Levesque 5,681 Reputation points MVP
    2022-05-05T20:45:53.24+00:00

    There isnt much way, Get-ADDomainController -Discover is the comdlet you would need to use, it use the DCLocator method to find a Domain Controller, but how you will manage the -Credential part ? as you can't run the powerscript against a DC from a workgroup computer as the process is run within the netlogon's process.

    You would maybe need to try to join the domain if it fail, then act accordingly.

    $result = Add-Computer -DomainName "myDomain" -ErrorAction SilentlyContinue -ErrorVariable ComputerError
    Write-Host $ComputerError[0]

    Your script might need a lot of work as for other computer you need to point the DNS's server of the other server to that newly DC to have the Get-ADDomainController & Add-Computer cmdlet to work out.

    Be aware that you can have multiple DC in the same LAN, the only difference to what DC answer what workstation is the DNS set inside the network adapter of the workstation to be able to use the correct's one.

    0 comments No comments

  2. Rich Matheisen 44,776 Reputation points
    2022-05-05T21:19:24.757+00:00

    I'm going to assume that "ABC.COM" is the name of the domain and not the name of the machine, AND that you know which DNS servers IN YOUR LAN to use (you better not find this information in publicly available DNS)!

    Get-DnsServerResourceRecord -ComputerName <A-DNS-SERVER> -Type SRV  -Name ABC.COM -Zone _ldap._tcp.pdc
    

    That should get you the PDC address.

    0 comments No comments

  3. Limitless Technology 39,351 Reputation points
    2022-05-11T07:12:59.393+00:00

    Hi PeterRietmann-0218,

    You can use PowerShell to find FSMO roles in an Active Directory using Get-AdForest cmdlet to get Schema master and Domain Name master roles. and using Get-AdDomain cmdlet to get PDCEmulator, RIDMaster, InfrastructureMaster roles. This may achieve what you're looking for in a different way.

    You can find FSMO roles in an Active Directory forest using the below command:

    Get-ADForest | Select-Object DomainNamingMaster, SchemaMaster
    Get-AdForest command gets Domain name master and schema master FSMO roles in active directory forest.

    To get domain FSMO roles, use the below command

    Get-ADDomain | Select-Object InfrastructureMaster, RIDMaster, PDCEmulator
    Get-AdDomain command gets domain FSMO roles like RID master, PCD emulator, and Infrastructure master.


    --If the reply is helpful, please Upvote and Accept as answer--