Skip to content

Instantly share code, notes, and snippets.

@Jackbennett
Forked from anonymous/scan AD computers.ps1
Created September 26, 2017 13:20
Show Gist options
  • Save Jackbennett/11ef97d7b244e06dc11a044445048115 to your computer and use it in GitHub Desktop.
Save Jackbennett/11ef97d7b244e06dc11a044445048115 to your computer and use it in GitHub Desktop.
Improve the run performance of this script.
# Get All PCs in Domain, Export to ServerList.txt
# Call get-inventory.ps1 logic on created Serverlist.txt
# dk / 22.09.2017
import-module activedirectory
$domain = "carpentier.local"
$tempfile = "c:\temp\tmpfile.csv"
$outfile = "c:\temp\Serverlist.csv"
Get-ADComputer -Filter * |
select-object Name |
Export-CSV -NoTypeInformation $tempfile
#Clean up outfile
$clean = Get-Content $tempfile
$clean[1..$clean.count] |
Set-Content $tempfile
cat $tempfile |
%{$_ -replace '"',''} |
Set-Content $outfile
# Do the magic
$servers = Get-Content $outfile
$infoColl = @()
Foreach ($s in $servers)
{
$CPUInfo = Get-WmiObject Win32_Processor -ComputerName $s #Get CPU Information
$OSInfo = Get-WmiObject Win32_OperatingSystem -ComputerName $s #Get OS Information
#Get Memory Information. The data will be shown in a table as MB, rounded to the nearest second decimal.
$OSTotalVirtualMemory = [math]::round($OSInfo.TotalVirtualMemorySize / 1MB, 2)
$OSTotalVisibleMemory = [math]::round(($OSInfo.TotalVisibleMemorySize / 1MB), 2)
$PhysicalMemory = Get-WmiObject CIM_PhysicalMemory -ComputerName $s | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) }
Foreach ($CPU in $CPUInfo)
{
$infoObject = New-Object PSObject
#The following add data to the infoObjects.
Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $CPU.SystemName
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Processor" -value $CPU.Name
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Model" -value $CPU.Description
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Manufacturer" -value $CPU.Manufacturer
Add-Member -inputObject $infoObject -memberType NoteProperty -name "PhysicalCores" -value $CPU.NumberOfCores
Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU_L2CacheSize" -value $CPU.L2CacheSize
Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU_L3CacheSize" -value $CPU.L3CacheSize
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Sockets" -value $CPU.SocketDesignation
Add-Member -inputObject $infoObject -memberType NoteProperty -name "LogicalCores" -value $CPU.NumberOfLogicalProcessors
Add-Member -inputObject $infoObject -memberType NoteProperty -name "OS_Name" -value $OSInfo.Caption
Add-Member -inputObject $infoObject -memberType NoteProperty -name "OS_Version" -value $OSInfo.Version
Add-Member -inputObject $infoObject -memberType NoteProperty -name "TotalPhysical_Memory_GB" -value $PhysicalMemory
Add-Member -inputObject $infoObject -memberType NoteProperty -name "TotalVirtual_Memory_MB" -value $OSTotalVirtualMemory
Add-Member -inputObject $infoObject -memberType NoteProperty -name "TotalVisable_Memory_MB" -value $OSTotalVisibleMemory
$infoObject #Output to the screen for a visual feedback.
$infoColl += $infoObject
}
}
#Export the results in csv file in c:\temp.
$infoColl | Export-Csv -path c:\temp\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment