Need help for Code optimization

Need help for Code optimization

I have written following code for gathering inventory based on IP address range. However it takes longtime to gather all information since it goes one by one to each machines. Is it possible to optimize following code to all multi-threaded execution?

Thanks you!
Abhay


function inventory {
PROCESS {
If (Test-Connection $ip -quiet -count 2 -TimeToLive 2)
{
Write-output “Ping Completed - $ip” | Out-File c:\PingStatus.txt -Append
$cs = gwmi Win32_ComputerSystem -ErrorAction SilentlyContinue -comp $_ | select *

            if ($?) {
                    $wp = gwmi Win32_Processor -ErrorAction SilentlyContinue -comp $_ | select Name , NumberOfCores -Unique    
                 }
            else {
                    $cs = gwmi Win32_ComputerSystem -ErrorAction SilentlyContinue -comp $_  -Credential $Credential | select * 
                    $wp = gwmi Win32_Processor -ErrorAction SilentlyContinue -comp $_ -Credential $Credential | select Name , NumberOfCores -Unique                         
                   
                    if ($? -eq 'false'){
                       Write-output "Check Machine for WMI access - $ip." | Out-File c:\PingStatus.txt -Append
                    }

                }
    
            $Prop= [ordered] @{
                'Computer Name'=$cs.pscomputername
                'Domain'=$cs.Domain
                'Memory'=$cs.TotalPhysicalMemory / 1gb -as [int]
                'Number Of Processors'=$cs.NumberOfProcessors
                'Manufacturer'=$cs.Manufacturer
                'Model #' =$cs.Model
                'Processor'= ($wp.Name -replace '   ',"")
                'Number Of Cores'=$wp.NumberofCores
                'Number Of Logical Processors'=$cs.NumberOfLogicalProcessors
                'Ip Address'=$ip
                }

            $obj= New-Object -TypeName psobject -Property $Prop
            write-output $obj                     
                                         
        }
 Else
    {
        Write-Output "Ping Failed $ip" | Out-File c:\PingStatus.txt -Append
    }
  
}

}

$user = “xyzdomain\User1”
$pass = Get-Content C:\securestring.txt | convertto-securestring
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass

$scriptBlock =
{
Write-output “Ping Status” | Out-File c:\PingStatus.txt
Get-date | Out-File c:\PingStatus.txt -Append

1..255 | foreach { $ip="10.185.60.$_" ; "$ip" | inventory }
1..255 | foreach { $ip="10.185.61.$_" ; "$ip" | inventory }
1..255 | foreach { $ip="10.229.160.$_"; "$ip" | inventory }
1..255 | foreach { $ip="10.229.161.$_"; "$ip" | inventory }

Get-date | Out-File c:\PingStatus.txt -Append    

}

&$scriptBlock| Sort-Object -Unique ‘Computer Name’ |Export-Csv ‘c:\inventory.csv’ -NoTypeInformation

Consider using the background job subsystem. You could start several copies of the script as background jobs, with each copy handling a group of your computers. Look at Start-Job as a beginning point.