PowerShell: Discover Environment

Discovery of Active Directory Computers

# Scan for all Windows machines in Active Directory
PS C:\Windows\system32> Get-ADComputer -Filter "OperatingSystem -like 'Windows*'" -Properties OperatingSystem | group operatingsystem | sort name

Count Name Group
----- ---- -----
270 Windows 10 Enterprise
1 Windows 10 Enterprise ...
18 Windows 10 Enterprise N
110 Windows 10 Pro
1 Windows 10 Pro Education
45 Windows 7 Enterprise
49 Windows 7 Professional
1 Windows 7 Ultimate
1 Windows 8 Pro
168 Windows 8.1 Enterprise
13 Windows 8.1 Pro
1 Windows Embedded Standard
2 Windows Server 2003
1 Windows Server 2008 R2...
5 Windows Server 2008 R2...
6 Windows Server 2008 R2...
8 Windows Server 2012 Da...
75 Windows Server 2012 R2...
15 Windows Server 2012 R2...
9 Windows Server 2016 Da...
16 Windows Server 2016 St...
13 Windows Server 2019 Da...
23 Windows XP Professional

# Narrow down to systems requiring removal such as Windows 2003 & XP
$win2003Servers=(Get-ADComputer -Filter "OperatingSystem -like 'Windows Server 2003'" -Properties OperatingSystem).DNSHostname
$winXPMachines=(Get-ADComputer -Filter "OperatingSystem -like 'Windows XP*'" -Properties OperatingSystem).DNSHostname

Discovery of Computer Resources

# Purpose: this script uses a list of computer names to discover information about each of them (IPs, Machine Type, CPU, Memory, and Disks)

$computerNames=@(
'SHERVER01',
'SHERVER03',
'SHERVER03'
)

$GLOBAL:computerObjects=@()

$scriptName=$MyInvocation.MyCommand.Path
$scriptPath=Split-Path -Path $scriptName

function systemsDiscovery{
#for ($i=0;$i -le $computers.length;$i++){
foreach($name in $computerNames){
"Scanning $name ..."
$computerObject=New-Object PSObject
$ips="Unknown";
$machineType="Unknown";
$cpu="Unknown";
$cpuLoad=0;
$memory="Unknown";
$volumes="Unknown";
try{
$ips=[System.Net.Dns]::GetHostAddresses($name)|?{$_.AddressFamily -eq "InterNetwork"}|%{$_.IPAddressToString;}
$machineModel=(Get-WmiObject -Class Win32_ComputerSystem -ComputerName $name -ea stop).Model
$cpu = Get-WmiObject -computername $name win32_processor -ea stop |select Name,NumberOfCores
$cpuLoad = Get-WmiObject -computername $name win32_processor -ea stop| Measure-Object -property LoadPercentage -Average | Select @{Name="CurrentLoad";Expression={"{0:N2} %" -f ($_.Average)}}
$memory = gwmi -Class win32_operatingsystem -computername $name -ea stop| select @{Name="Memory";Expression={"{0:N2} GB" -f ($_.TotalVisibleMemorySize / 1048576)}},@{Name = "MemoryUsage"; Expression = {"{0:N2} %" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
#$volumes = (gwmi -Class win32_volume -computername $name -ea stop| ?{$_.DriveLetter -ne $isnull}|Select-object DriveLetter,Capacity,@{Name = "PercentFree"; Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) } } | Out-String ).TrimEnd()
$volumes = (gwmi -Class win32_volume -computername $name -Filter "DriveType!=5" -ea stop| ?{$_.DriveLetter -ne $isnull}|Select-object DriveLetter,@{Name="Capacity";Expression={"{0:N2} GiB" -f ($_.Capacity/1073741824)}},@{Name = "PercentFree"; Expression = {"{0:N2} %" -f (($_.FreeSpace / $_.Capacity)*100) } } | Out-String ).Trim()
}
catch{
$machineModel="Unknown";
$machineType="Unknown";
if(!($ips)){$ips="Unknown";}
if(!($cpu)){$cpu="Unknown";}
if(!($memory)){$memory="Unknown";}
if(!($volumes)){$volumes="Unknown";}
Continue;
}
finally{
switch -wildcard ($machineModel){
"*Virtual*" {$machineType="VMWare Virtual Machine";}
"*HVM*" {$machineType="AWS Virtual Machine";}
"Unknown" {$machineType="Physical Machine";}
}
$computerObject | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
$computerObject | Add-Member -MemberType NoteProperty -Name "ips" -Value $ips
$computerObject | Add-Member -MemberType NoteProperty -Name "machineType" -Value $machineType
$computerObject | Add-Member -MemberType NoteProperty -Name "cpu" -Value $cpu,$cpuLoad
$computerObject | Add-Member -MemberType NoteProperty -Name "memory" -Value $memory
$computerObject | Add-Member -MemberType NoteProperty -Name "volumes" -Value $volumes
$computerObject
$GLOBAL:computerObjects+=,$computerObject
}
}
}

systemsDiscovery;

"Exporting results to a CSV file in the current path of $scriptPath`..."
$computerObjects|Export-Csv -Path "$scriptPath`\systemsDiscovery.csv" -NoTypeInformation

Leave a Reply

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