Reputation: 97
This self-answered question addresses the following scenario:
How can I write a PowerShell script to check if a computer is a domain controller or not?
Upvotes: 3
Views: 4439
Reputation: 519
The Win32_OperatingSystem
WMI class property ProductType
will tell you if the computer is a workstation, domain controller or server:
ProductType
Data type: uint32
Access type: Read-only
Additional system information.
Work Station (1)
Domain Controller (2)
Server (3)
$productType = (Get-CimInstance -ClassName Win32_OperatingSystem).ProductType
$cn = $env:COMPUTERNAME
switch ($productType)
{
1 {"'$cn' is a workstation."; break}
2 {"'$cn' is a domain controller."; break}
3 {"'$cn' is a server."; break}
}
Upvotes: 2
Reputation: 97
The code below can be run on Windows PowerShell. It will take an input list of computers called computers.csv and loop around them to check if it is a domain controller or not and then output the result into check_for_domain_controller.csv
$listofcomputers = Import-CSV -Path "C:\computers_list.csv"
foreach ($computerobject in $listofcomputers)
{
$computername = $computerobject.Name
Get-DomainRole -Computername $computername |
Export-csv -Path "C:\check_for_domain_controller.csv" -Append -NoTypeInformation
}
Input (computers.csv)
Name
DC1
DC2
DC3
DC4
PC1
PC2
Output (check_for_domain_controller.csv)
"Computer","IPAddress","PCType","DomainRole"
"DC1","10.10.10.1","Desktop","Domain controller"
"DC2","110.10.10.2","Desktop","Domain controller"
"DC3","10.10.10.3","Desktop","Domain controller"
"DC4","10.10.10.4","Desktop","Domain controller"
"PC1","10.10.10.5","Desktop","Member server"
"PC2","10.10.10.6","Desktop","Member server"
Upvotes: -2
Reputation: 3923
Yes, if you need to check a list then that's fine, but if all you need is a list of DCs, then more efficient to get this directly.
$DomainName = 'MyDomain'
$DomDetail = Get-ADDomain -Identity $DomainName
$DCDetail = Get-ADDomainController -Server $DomDetail.PDCEmulator -Filter *
[pscustomobject]@{Name = $DomDetail.NetBIOSName;FQDN = $DomDetail.DNSRoot;PDC = $DomDetail.PDCEmulator;MemberDCs = $DCDetail}
Upvotes: 2