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: Find Hyper-V Host by Guest VM Name

# findVmHostByGuestName.ps1 $vmName='TESTVM' function findVmHostByGuestName($vmName){ try{ Import-Module Hyper-V Import-Module FailoverClusters $allHyperVHosts={(Get-ClusterNode | Where { $_.State…

How To Upgrade NextCloud 22.1.1 to 22.2.0 When Deployed with Kubernetes & Helm

Step 1: Navigate to nextcloud > html > edit version.php <?php $OC_Version = array(22,1,1,2); $OC_VersionString…

Convert LastLogon Date From Number to Date Time

We have a case where our Admins have generated a CSV file with information about…
Other project: DragonCoin.com