Developer42

2014-08-22

Powershell Script: Get Server Inventory

Filed under: Microsoft — Tags: , , , , , , , , , , — Developer42 @ 16:36

**Update 2017-02-05**

This script is now available on GitHub: https://github.com/JohnLBevan/PSForInfrastructure/, where it will be kept updated with functionality requests and refactored to make it easier for you to adapt this script to meet your requirements through parameters.


The following script (heavily based on http://www.the-fays.net/blog/?p=334) polls all servers in a given text file, returning information about their disk capacity, # of cpus (sockets & cores), amount of RAM, and OS info.


$serverList = ".\Servers.txt"
$outputCSV = ".\ServerInventory.csv"


$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
pushd $dir

[System.Collections.ArrayList]$sysCollection = New-Object System.Collections.ArrayList($null)
 
foreach ($server in (Get-Content $serverList))
{
    "Collecting information from $server"
    $totCores=0
 
    try
    {
        [wmi]$sysInfo = get-wmiobject Win32_ComputerSystem -Namespace "root\CIMV2" -ComputerName $server -ErrorAction Stop
        [wmi]$bios = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -computername $server
        [wmi]$os = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -Computername $server
		#[array]$disks = Get-WmiObject Win32_LogicalDisk -Namespace "root\CIMV2" -Filter DriveType=3 -Computername $server
		[array]$disks = Get-WmiObject Win32_LogicalDisk -Namespace "root\CIMV2" -Computername $server
		[array]$procs = Get-WmiObject Win32_Processor -Namespace "root\CIMV2" -Computername $server
        [array]$mem = Get-WmiObject Win32_PhysicalMemory -Namespace "root\CIMV2" -ComputerName $server
        [array]$nic = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ComputerName $server | where{$_.IPEnabled -eq "True"}
 
        $si = @{
			Server			= [string]$server
			Manufacturer	= [string]$sysInfo.Manufacturer
			Model			= [string]$sysInfo.Model
			TotMem			= "$([string]([System.Math]::Round($sysInfo.TotalPhysicalMemory/1gb,2))) GB"
			BiosDesc		= [string]$bios.Description
			BiosVer			= [string]$bios.SMBIOSBIOSVersion+"."+$bios.SMBIOSMajorVersion+"."+$bios.SMBIOSMinorVersion
			BiosSerial		= [string]$bios.SerialNumber
			OSName			= [string]$os.Name.Substring(0,$os.Name.IndexOf("|") -1)
			Arch			= [string]$os.OSArchitecture
			Processors		= [string]@($procs).count
			Cores			= [string]$procs[0].NumberOfCores
			
		}
		
		$disks | foreach-object {$si."Drive$($_.Name -replace ':', '')"="$([string]([System.Math]::Round($_.Size/1gb,2))) GB"}
    }
    catch [Exception]
    {
        "Error communicating with $server, skipping to next"
        $si = @{
			Server			= [string]$server
			ErrorMessage	= [string]$_.Exception.Message
			ErrorItem		= [string]$_.Exception.ItemName
		}
        Continue
    }
    finally
    {
       [void]$sysCollection.Add((New-Object PSObject -Property $si))   
    }
}
 
$sysCollection `
	| select-object Server,TotMem,OSName,Arch,Processors,Cores,Manufacturer,Model,BiosDesc,BiosVer,BiosSerial,DriveA,DriveB,DriveC,DriveD,DriveE,DriveF,DriveG,DriveH,DriveI,DriveJ,DriveK,DriveL,DriveM,DriveN,DriveO,DriveP,DriveQ,DriveR,DriveS,DriveT,DriveU,DriveV,DriveW,DriveX,DriveY,DriveZ,ErrorMessage,ErrorItem `
	| sort -Property Server `
	| Export-CSV -path $outputCSV -NoTypeInformation    
	

Servers.txt should be held in the same directory as the script, & would look something like this:

MyServer1
MyServer2.fully.qualified.domain.com
YetAnotherServerName
172.24.8.1

Output looks something like this:

"Server","TotMem","OSName","Arch","Processors","Cores","Manufacturer","Model","BiosDesc","BiosVer","BiosSerial","DriveA","DriveB","DriveC","DriveD","DriveE","DriveF","DriveG","DriveH","DriveI","DriveJ","DriveK","DriveL","DriveM","DriveN","DriveO","DriveP","DriveQ","DriveR","DriveS","DriveT","DriveU","DriveV","DriveW","DriveX","DriveY","DriveZ","ErrorMessage","ErrorItem"
"MyServer1","8 GB","Microsoft Windows Server 2003 R2 Enterprise Editio","","4","","HP","ProLiant DL380 G5","Default System BIOS","P56.2.4","CZC8924LQ6      ","","","68.33 GB","0.58 GB","683.5 GB","0 GB","","","","","","","","","","","","","","","","","","","","","",""
"MyServer2.fully.qualified.domain.com","48 GB","Microsoft Windows Server 2012 Datacente","64-bit","8","1","VMware, Inc.","VMware Virtual Platform","PhoenixBIOS 4.0 Release 6.0     ","6.00.2.4","VMware-42 3b 8b d9 d3 92 b3 8e-9a 43 b1 b5 e6 a8 b2 74","0 GB","","31.66 GB","99.87 GB","3.63 GB","","","","","","","99.87 GB","","","","49.87 GB","","","","19.87 GB","","","","","","","",""
"YetAnotherServerName","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))",""
"172.24.8.1","48 GB","Microsoft Windows Server 2012 Datacente","64-bit","8","1","VMware, Inc.","VMware Virtual Platform","PhoenixBIOS 4.0 Release 6.0     ","6.00.2.4","VMware-42 3b 8b d9 d3 92 b3 8e-9a 43 b1 b5 e6 a8 b2 74","0 GB","","31.66 GB","99.87 GB","3.63 GB","","","","","","","99.87 GB","","","","49.87 GB","","","","19.87 GB","","","","","","","",""
Advertisements

12 Comments »

  1. thanks this works very well, i’m trying to add OS service pack to the list, some help on this please? thanks,

    Comment by brandon — 2015-02-21 @ 17:32

    • Before:
      Arch = [string]$os.OSArchitecture
      add:
      ServicePack = (“SP {0}” -f [string]$os.OSArchitecture)

      Then update:
      select-object Server,TotMem,OSName,Arch,
      to
      select-object Server,TotMem,OSName,ServicePack,Arch,

      Comment by Developer42 — 2015-02-23 @ 21:44

  2. thanks for this, I’ve tried it but only get “SP 64-bit” as a result. also under the error message column of the csv output, i’m getting access is denied, these i know are non domain-joined vm’s whose local administrator account is the only one in existence, i’m running my script from a domain admin profile, how would i be able to runas localadmin account for the vm’s not yet domain joined?

    Comment by brandon — 2015-02-26 @ 09:33

    • Sorry, typo on my part.
      ServicePack = (“SP {0}” -f [string]$os.OSArchitecture)
      should be
      ServicePack = (“SP {0}” -f [string]$os.ServicePackMajorVersion)

      FYI: You can see everything that’s available on OS by running the below code:

      [wmi]$os = Get-WmiObject Win32_OperatingSystem -Namespace “root\CIMV2”
      $os | select *

      Comment by Developer42 — 2015-02-26 @ 19:21

  3. Thanks for the beautiful script, unfortunately Windows 2012 servers we could not able to get the details,

    Getting The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    Comment by Mohammed Afzal — 2016-05-10 @ 12:12

  4. Can we get free disk size on each drices as well as total size of hard-disk and total free space

    Comment by jack — 2016-06-02 @ 21:47

  5. Can we get Total Harddrive size and free space + free space in each harddrive

    Comment by jack — 2016-06-02 @ 23:18

  6. can i get Server description details from this script?Please help!!

    Comment by pankaj kumar — 2017-01-25 @ 05:18


RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: