Posted On July 1, 2019

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

PowerShell: 1-Liner to Change Computer Name and Join Active Directory

# The 1-liner Add-Computer -DomainName 'somedomain.com' -credential kimconnect.com\domainadmin1 -ComputerName $env:computernname -newname 'NewName' -restart # Quick…

Using Telnet and PowerShell to Test SMTP Relay

PowerShell Method: Example of Failure: PS C:\> sendTestEmail '[email protected]' 'password' '[email protected]'Detected MX Record : ywpjf4z5siycosmh7uqymtuygcjehuc67wa6o4rq4k2a3g2aodma.mx-verification.google.comKnown…

PowerShell: Technique to Pass Array Variable as Argument

Problem: The subtle difference between invoke-command -scriptblock {} -Args vs -ArgumentList is elusive enough to…