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: Get ‘Size on Disk’ of Files in Windows

There are many methods of attempting to obtain "size on disk" values of files in…

Installing IBM VPN Client

On a Linux Machine # Install VPN Client shellScript=https://support.arraynetworks.net/prx/001/http/supportportal.arraynetworks.net/downloads/pkg_9_4_0_385/MP_Linux_1.2.9/MotionPro_Linux_Ubuntu_x64_build-8.sh cd Desktop wget $shellScript sudo ./MotionPro_Linux_Ubuntu_x64_build-8.sh…

Basic JavaScript: Manipulate Arrays With shift()

// Setupvar myArray = [["John", 23], ["dog", 3]];// Only change code below this line.var removedFromMyArray=myArray.shift();