Categories

Scripting Games-don’t repeat the work

There are some good features to this script but what really hurts is the two trips to the server for the Win32_Computersystem class

Foreach ($IP in (Get-Content "C:\IPList.txt"))
{
  $Name = (Get-WMIObject Win32_ComputerSystem -ComputerName $ip).Name
  $Mem = [math]::truncate((Get-WMIObject Win32_ComputerSystem -ComputerName $ip).TotalPhysicalMemory / 1MB)
  $Ver = (Get-WMIObject Win32_OperatingSystem -ComputerName $ip).Caption
  [Array]$Cpus = (Get-WMIObject Win32_Processor -ComputerName $ip)
  $CpuCount = $Cpus.Count
  $Cores = 0
  Foreach ($Socket in $Cpus)
    {
    $Cores = $Cores + $Socket.NumberOfCores
    }
  "$Name,$Mem,$Ver,$CpuCount,$Cores" | Out-File output.csv -Append
}
 
it would be better to do this 
$comp = Get-WMIObject Win32_ComputerSystem -ComputerName $ip
$name = $comp.Name
$mem = [math]::truncate($comp.TotalPhysicalMemory / 1MB)
Dropping one round trip on a few servers isn’t that big a deal.  dropping it on 3000 servers will make a difference
Always think about how your scripts may need to scale one day

Leave a Reply