Powershell script for Servers inventory


Quite many months since when I might have written a blog post. Life been quite busy at workplace and home end both. Thought to share yet another PowerShell script, which might be quite useful for System Admins.

This covers CPU, Memory, Network cards, Disks etc and other parameters can be added accordingly.

You might run into issues with some servers if WMI related ports not open on them. Below CMD should help on that

netsh advfirewall firewall set rule group=”Windows Remote Management” new enable=yes

# Author : Nitish Kumar
# Outputs the results to a csv file. 
# version 1.0

# The conditions can be tweaked to find some specific set of computers, may be workstations or a particular OS or may be Security group member
$servers = Get-ADComputer -f {OperatingSystem -like "*Server*"} -properties IPv4Address, OperatingSystem | select-object Name, IPv4Address, OperatingSystem 

#Run the commands for each server in the list
$infoColl = @()
$i = 0
Foreach ($s in $servers)
{
	$i++
	Write-Progress -activity "Getting Server Details ($i / $($Servers.Count)):" -status "Percent Done: " -PercentComplete (($i / $Servers.Count)  * 100) -CurrentOperation "Now processing $($s.Name)"	

	try{
		$CPUInfo = Get-WmiObject Win32_Processor -ComputerName $s.Name 		
		$PhysicalMemory = Get-WmiObject CIM_PhysicalMemory -ComputerName $s.Name | Measure-Object -Property capacity -Sum | % { [Math]::Round(($_.sum / 1GB), 2) }
		$NetworkInfo = Get-wmiobject Win32_networkadapter -ComputerName $s.Name | ?{$_.MACAddress -ne $null -AND $_.PhysicalAdapter -eq $true}
		$DiskInfo = Get-wmiobject Win32_LogicalDisk -ComputerName $s.Name
		$SerialNumber = (Get-WmiObject Win32_BIOs -ComputerName $s.Name).SerialNumber
		$MakeInfo = Get-WmiObject Win32_ComputerSystem -ComputerName $s.Name
		$NICSpeed = (($NetworkInfo.Speed | %{([Math]::Round(($_ / 1GB), 2))}) -Join " Gbps,") + " Gbps"
		$DiskSizes = (($DiskInfo.size | %{([Math]::Round(($_ / 1GB), 2))}) -join " GB,") + " GB"
		$DiskFreeSizes = (($DiskInfo.FreeSpace | %{([Math]::Round(($_ / 1GB), 2))}) -join " GB,") + " GB"

		$infoObject = New-Object PSObject
		#The following add data to the infoObjects.         
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $s.Name
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "IP Address" -value $s.IPv4Address
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "SerialNumber" -value $SerialNumber
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Manufacturer" -value $MakeInfo.Manufacturer
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Model" -value $MakeInfo.Model
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "OperatingSystem" -value $s.OperatingSystem
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Processor" -value ($CPUInfo.Name -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU Model" -value ($CPUInfo.Description -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU Manufacturer" -value ($CPUInfo.Manufacturer -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU PhysicalCores" -value ($CPUInfo.NumberOfCores -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU LogicalCores" -value ($CPUInfo.NumberOfLogicalProcessors -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "OS_Name" -value $OSInfo.Caption		
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "TotalPhysical_Memory_GB" -value $PhysicalMemory	
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC count" -value ($NetworkInfo | Measure-object).Count
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC Name" -value ($NetworkInfo.NetConnectionID -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC Type" -value ($NetworkInfo.ProductName -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC Manufacturer" -value ($NetworkInfo.Manufacturer -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC MAC Address" -value ($NetworkInfo.MACAddress -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "NIC Speed" -value $NICSpeed
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Drives Count" -value ($DiskInfo | Measure-object).Count
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Drives Letters" -value ($DiskInfo.DeviceID -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Drive Volume Names" -value ($DiskInfo.VolumeName -join ",")
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Drive Sizes" -value $DiskSizes
		Add-Member -inputObject $infoObject -memberType NoteProperty -name "Drive free space Sizes" -value $DiskFreeSizes
			
		$infoObject #Output to the screen for a visual feedback.
		$infoColl += $infoObject
	}
	catch {
		Write-host "Issue in data collection from $($s)"
		Continue
	}	
}
$infoColl | Export-Csv -path .\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation #Export the results in csv file.

Author: Nitish Kumar

I love to write and raising voice, sharing thought and heated debate is a kind of passion for me. Jobwise I am just another Computer professional handling Infra and designing solutions for a big Indian Media house but I love to write, sketch, photography and a lot more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.