header image

Scripting Games-don’t repeat the work

Posted by: | May 7, 2013 | No Comment |

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
under: PowerShell and WMI, PowerShell original, Scripting Games