Thursday, October 20, 2011

Powershell for Server Inventory

Here are a few snippets I have built to pull inventory information

To gather information where each request will only have one result per machine (doesn't work for things like disk or processor where there can be multiple values for one machine)

#computers.txt should be a list of computers to gather information from. You could also build an AD query instead if you want.
get-Content computers.txt | foreach {
$os=Get-WmiObject -class Win32_OperatingSystem -ComputerName $_
$sys=Get-WmiObject -Class win32_Computersystem -ComputerName $_
$bios=Get-WmiObject -Class win32_Bios -ComputerName $_
$net=Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $_
$disk=Get-WmiObject -Class win32_logicaldisk
$IP=[System.Net.Dns]::GetHostAddresses($_)| select-object ipaddresstostring

#create a new object so that we can output our data to CSV neatly
#This script returns computer name, OS, memory, vendor, model, domai, serial number, and service tag
new-Object psobject -Property @{
Computername=$_.toUpper()
OperatingSystem=$os.Caption
TotalPhysMemMB=($sys.TotalPhysicalMemory)/1MB -as [int]
Vendor=$sys.Manufacturer
Model=$sys.model
Domain=$sys.domainget
SerialNumber=$os.serialnumber
ServiceTag=$bios.SerialNumber
}| convertto-csv -notypeinfo -outvariable OutData
$outData[1..($outdata.count -1)]|foreach-object {add-content -value $_ -Path OSInfo.csv}
}

This one checks the ping status of each machine in a list of machines. If it is able to ping, it writes that value to a CSV file. If you took out the where-object, you could just have it return the server name and the ping status value instead.

get-content tcmis.txt | ForEach{Get-WmiObject win32_pingstatus -Filter "Address='$_'" | Where-object {$_.statuscode -eq 0} |select-object address, statuscode} | export-csv -notypeinfo exist.csv

Once you have your list of machines that exist, this script can get all the hard drive freespace and size information. It only pulls from non-removable drives (drive type 3)

#Exist.CSV contains a list of computer names. Find all non-removable drive types and export the information to a csv file

Get-content exist.csv | Foreach {Get-WmiObject -Class win32_logicaldisk -ComputerName $_ | where-object {$_.drivetype -eq 3} | select-object systemname, deviceID, FreeSpace, Size}| export-csv diskinfo.csv -notypeinfo

1 comment:

Parasuram K said...

thank you for the script. I would like to capture the IP address in the output with the other system details like OS, serial no etc. could you please guide