How to scan ip addresses range and get important details with Powershell GUI - Part 3

Today i come back with my Powershell script that try to automate a tasks that i think all IT Pro has diffculties when need a Report for all Workstation & Servers and details like Ram,Cpu,Disks .....

In the market you can find lot of these software's that can help you to keep up to date your Reports but why not use our knowledge to find the way and automate this task.

Last months i work in a Powershell script to retrieve all these kind of info (cpu,ram,disks .....) base on Ip Range that you will give.

To be honest it's not so easy as i think and i have face issues that not have resolve yet.

You can find my previous script here if you want or read my last article for the script  How to scan ip addresses range and get important details with Powershell GUI - Part 2

You can find the new script in Microsoft Technet in the following link

You can download the Script from here

One of my biggest issue in the previous script was that very slow when scan to retrieve details with  Wmi technique.

So i decide to change it and use the Invoke-Command with Wmi for faster results.

For example the time that need for 10 ip addresses to scan (some of them was unreachable) with  Wmi it takes 2-3 minutes

Now with Invoke-Command and Wmi for the same number of ip addresses need only 40-50 seconds.

So let's explain the code that change.

In this lines of previous script use a loop to scan the ip range , create an object and retrieve details with Get-Wmi command

$a..$b | ForEach-Object {$o = new-object  psobject # New object
 $o | add-member -membertype noteproperty -name ipaddress -value (gwmi Win32_PingStatus -Filter "Address='$c.$_'").ProtocolAddress | Out-Null
 $o | add-member -membertype noteproperty -name OS -value (gwmi Win32_Operatingsystem -computername "$c.$_").Caption | Out-Null
 $o | add-member -membertype noteproperty -name Computername -value (gwmi Win32_ComputerSystem -computername "$c.$_").Caption  | Out-Null
 $o | add-member -membertype noteproperty -name TotalPhysicalram -value (gwmi Win32_ComputerSystem -computername "$c.$_").TotalPhysicalMemory | Out-Null
 $o | add-member -membertype noteproperty -name CPU -value (gwmi Win32_processor -computername "$c.$_").Caption | Out-Null
 $o | add-member -membertype noteproperty -name Localdisk -value (gwmi Win32_Logicaldisk -filter "deviceid='C:'" -computername "$c.$_").Size | Out-Null
 
 $arr += $o
}

Now i have replace these lines with the following:

  • Because Invoke-Command understand only Computer name and not Ip Addresses. You can use and Ip Addresses with Invoke-command but you must do lot of changes and i don't want to. 
  • I have create a loop to retrieve only the computer name from the ip address and export to csv file

$a..$b |  ForEach-Object {  $address="$c.$_"     if (Test-Connection -Cn $address -Count 1 -Quiet  )
{
$o = new-object  psobject
$o | add-member -membertype noteproperty -name OS -value (gwmi Win32_Computersystem -computername $address).Caption | Out-Null

}

 else
 {
     $o = new-object  psobject
     $o | add-member -membertype noteproperty -name OS -value "not found"
}

   $arr1 += $o
   }

   $arr1 |  Export-Csv C:\ip.csv 

 

  • After the csv file created i use a 2nd loop with Invoke-Command that read the csv file to retrieve the computer names and scan to get the info with Get-Wmi.

$computername=import-csv c:\ip.csv

 $arr=@()

 Foreach($pc in $computername)
 {
   $ip=$pc.OS

    if (Test-Connection -Cn $ip -Count 1 -Quiet  )
    {
         $r=@()
        

  Invoke-Command -cn $ip -Credential $cred -ScriptBlock 
  { 
  
$r=New-Object psobject
$r | add-member -membertype noteproperty -name OS -value (gwmi Win32_Operatingsystem ).Caption | Out-Null
$r | add-member -membertype noteproperty -name Computername -value (gwmi Win32_ComputerSystem ).Caption  | Out-Null
$r | add-member -membertype noteproperty -name TotalPhysicalram -value (gwmi Win32_ComputerSystem ).TotalPhysicalMemory | Out-Null
$r | add-member -membertype noteproperty -name CPU -value (gwmi Win32_processor ).Caption | Out-Null
$r | add-member -membertype noteproperty -name Localdisk -value (gwmi Win32_Logicaldisk -filter "deviceid='C:'" ).Size | Out-Null

    $arr+=$r
    $arr| Select-Object -Property os,computername,TotalPhysicalram,CPU,Localdisk

  }     
     
 }
}

 

The results are show only in Powershell because i face the issue to retrieve the array from Invoke-Command and import to datagridview.

I am working to resolve the issue and i will come back very soon with the updated version.

Have a nice weekend !!

I invite you to follow me on Twitter , Google+ or Facebook. If you have any questions, send email to me at info@askme4tech.com.