Thursday, December 21, 2017

How to Get Systems Inventory i.e. RAM, Disk, CPU and Operating systems information using Powershell in domain.

Every System Administrator needs to maintain Server Inventory and keep it update, So here is an automated script to pull all the information using Powershell script. 

Let's copy the script below and save it as Server_Inventory.PS1.
If you have few servers you can define name manually or you can read N number of the system from a file.   


Create Pc.txt file as in below format

Server1
Server2
Server2

Copy the Script and Save as Server_Inventory.PS1.
#Define Systems Name or you can read them from a file using command (Get-Content -Path D:\pc.txt)
$pc = (Get-Content D:\PC.txt) #change this path with your .txt file
$infocoll = @()
foreach ($PCS in $pc)
{
        #Here We can use multiple command to get the data

        $hostname = (Get-WmiObject -Class win32_processor -ComputerName $PCS).systemname 
        $HDDInfo = Get-WmiObject -Class win32_diskdrive -ComputerName $PCS | Measure-Object -Property size -Sum | % { [math]::Round(($_.sum / 1GB ), 2) }
        $RamInfo = Get-WmiObject -Class win32_physicalmemory -ComputerName $PCS | Measure-Object -Property Capacity -Sum | % {[math]::Round(($_.sum / 1GB), 2)}
        $Cdrive = Get-WmiObject -Class win32_logicaldisk -ComputerName $PCS |   where deviceid -EQ 'C:' 
        $CDrivePercentuse= (($Cdrive).size -($Cdrive).freespace)*100/($Cdrive).size
        $CpuInfo = (Get-WmiObject -Class win32_processor -ComputerName $PCS).Name
        $OSInfo = (Get-WmiObject -Class win32_operatingsystem -ComputerName $PCS).Name
       
        foreach($CPU in $hostname)
        {
$infoObject = New-Object PSObject
        #Define the Value to each objet you want to add into the .CSV file.

        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "ServerName" -Value $hostname
        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "HDD SIZE in GB" -Value $HDDInfo
        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "C Drive Use IN %" -Value $CDrivePercentuse
        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "CPU Detail" -Value $CpuInfo
        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "RAM Detail in GB" -Value $RamInfo
        Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "O.S Name" -Value $OSInfo
       
        $infoObject
        $infocoll += $infoObject
        
        }
    }
 # Export all the information to a .CSV file and store the somewhere else you want.
 $infocoll | Export-Csv -Path "D:\Serverinvt.csv" -NoTypeInformation


Run the Script and you will get output in a CSV file as in the example below.




Thank you All!!


No comments:

Post a Comment