Wednesday, March 19, 2014

Computer Hardware Inventory

# On error the script will continue silently without
$erroractionpreference = "SilentlyContinue"
# TXT file containing the computers your pinging

$testcomputers = gc -Path "path to the text file where computers list is located"


$test_computer_count = $testcomputers.Length;

$x = 0;

write-host -foregroundcolor cyan ""

write-host -foregroundcolor cyan "Testing $test_computer_count computers, this may take a while."

foreach ($computer in $testcomputers) {

        # I only send 2 echo requests to speed things up, if you want the defaut 4

        # delete the -count 2 portion

   if (Test-Connection -ComputerName $computer -Quiet -count 2){

 

        Add-Content -value $computer -path The path to the livePCs.txt file
# example d:\output\livePCs.txt

        }else{



        Add-Content -value $computer -path The path to the deadPCs.txt file
# example d:\output\deadPCs.txt

        }

    $testcomputer_progress = [int][Math]::Ceiling((($x / $test_computer_count) * 100))

    # Progress bar

    Write-Progress  "Testing Connections" -PercentComplete $testcomputer_progress -Status "Percent Complete - $testcomputer_progress%" -Id 1;

    Sleep(1);

    $x++;



}

write-host -ForegroundColor cyan ""

write-host -foregroundcolor cyan "Testing Connection complete"

write-host -foregroundcolor cyan ""



$ComputerName = gc -Path "d:\output\livePCs.txt"



$computer_count = $ComputerName.Length;

# The results of the script are here Change as per your need

$exportLocation = "d:\output\pcInventory-$((Get-Date -uformat %Y%m%d).ToString()).csv"

$i = 0;

 foreach ($Computer in $ComputerName){

   $Bios =get-wmiobject win32_bios -Computername $Computer

   $Hardware = get-wmiobject Win32_computerSystem -Computername $Computer

   $Sysbuild = get-wmiobject Win32_WmiSetting -Computername $Computer

   $OS = gwmi Win32_OperatingSystem -Computername $Computer

   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}

   $driveSpace = gwmi win32_volume -computername $Computer -Filter 'drivetype = 3' |

   select PScomputerName, driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f($_.freespace/1GB)} } |

   Where-Object { $_.driveletter -match "C:" }
 
   $cpu = Get-WmiObject Win32_Processor  -computername $computer

   $username = Get-ChildItem "\\$computer\c$\Users\" | Sort-Object LastWriteTime -Descending | Select Name, LastWriteTime -first 1

   $totalMemory = [math]::round($Hardware.TotalPhysicalMemory/1024/1024/1024, 2)

   $lastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime)



   #write-host -foregroundcolor yellow "Found $computer"

   $computer_progress = [int][Math]::Ceiling((($i / $computer_count) * 100))

    # Progress bar

    Write-Progress  "Gathering Hardware Info" -PercentComplete $computer_progress -Status "Percent Complete - $computer_progress%" -Id 1;

    Sleep(1);

    $i++;

   foreach ($Network in $Networks) {

    $IPAddress  = $Network.IpAddress[0]

    $MACAddress  = $Network.MACAddress

    $systemBios = $Bios.serialnumber

    $OutputObj  = New-Object -Type PSObject

    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()

    $OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Hardware.Manufacturer

    $OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Hardware.Model

    $OutputObj | Add-Member -MemberType NoteProperty -Name CPU_Info -Value $cpu.Name

    $OutputObj | Add-Member -MemberType NoteProperty -Name SystemType -Value $Hardware.SystemType

    $OutputObj | Add-Member -MemberType NoteProperty -Name BuildVersion -Value $SysBuild.BuildVersion

    $OutputObj | Add-Member -MemberType NoteProperty -Name OS -Value $OS.Caption

    $OutputObj | Add-Member -MemberType NoteProperty -Name SPVersion -Value $OS.csdversion

    $OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $systemBios

    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress

    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress

    $OutputObj | Add-Member -MemberType NoteProperty -Name UserName -Value $username.Name

    $OutputObj | Add-Member -MemberType NoteProperty -Name Last-Login -Value $username.LastWriteTime

    $OutputObj | Add-Member -MemberType NoteProperty -Name C:_GBfreeSpace -Value $driveSpace.GBfreespace

    $OutputObj | Add-Member -MemberType NoteProperty -Name Total_Physical_Memory -Value $totalMemory

    $OutputObj | Add-Member -MemberType NoteProperty -Name Last_Reboot -Value $lastboot

    $OutputObj | Export-Csv $exportLocation -Append

   }

}



 write-host -foregroundcolor cyan "Script is complete, the results are here: $exportLocation"

No comments:

Post a Comment