PowerShell: Discover Domain Controllers and Classify Each As VM or Physical

Current Version

$dcObjects=Get-ADDomainController -Filter * | select Name,OperationMasterRoles
foreach ($dc in $dcObjects){
$dcName=$dc.Name
$dcRole=$dc.OperationMasterRoles
try{
$machineModel=(Get-WmiObject -Class Win32_ComputerSystem -ComputerName $dcName -ea stop).Model
}
catch{
$machineModel="Unknown";
$machineType="Unknown";
Continue;
}
finally{
switch -wildcard ($machineModel){
"*Virtual*" {$machineType="Virtual Machine";}
"*HVM*" {$machineType="Virtual Machine";}
"Unknown" {$machineType="Physical Machine";}
}
if ([string]::IsNullOrEmpty($dcRole)){$dcRole="None"}
"$dcName`: $machineType - Roles: $dcRole"
}
}

Output:

AD02: Physical Machine - Roles: None
AD01: Physical Machine - Roles: None
AD03: Physical Machine - Roles: SchemaMaster DomainNamingMaster PDCEmulator RIDMaster InfrastructureMaster

Old Version:

# Adding Prerequisite Active Directory Module
if (!(get-module -name "ActiveDirectory") ){
Add-WindowsFeature RSAT-AD-PowerShell | out-null;
import-module -name "ActiveDirectory" -DisableNameChecking | out-null;
}

$allDcs=Get-ADDomainController -Filter * | Select Name
foreach ($dc in $allDcs){
$dcName=$dc.Name
$machineType=(Get-WmiObject -Class Win32_ComputerSystem -ComputerName $dcName -ErrorAction Continue).Model
if($machineType -like "Virtual Machine"){
"$dcName`: $machineType"
}else{
"$dcName`: Physical Machine"
}
# netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
}

Output:

DC01: Virtual Machine
DC02: Physical Machine

Leave a Reply

Your email address will not be published. Required fields are marked *