Monday, May 13, 2013

What I Learned at the Scripting Games

I'm competing in this years PowerShell Scripting Games 

They come up with a new problem each week, we have a week to write a script to solve that problem. Our scripts are submitted and reviewed by the crowd who can post comments. I've received some great feedback already!

The first script I submitted  received a mediocre reception. Which is totally fair looking at it, there are some real rookie errors in it. It’s great though because the point of the Scripting Games is to LEARN! We've got some of the top people in the powershell community critiquing our work! it's fantastic!

My Script ended up look like this;
$servers = c:\iplist.txt
$report = @()
foreach ($server in $servers)
       $temp = "" | Select Hostname, Cores, Memory
       $cores = Get-WmiObject win32_processor -ComputerName $server | Select numberofcores
       $memory = Get-WmiObject Win32_ComputerSystem -ComputerName $server | Select TotalPhysicalMemory
       $hostname = Get-WmiObject Win32_ComputerSystem -ComputerName $server | Select Name
       $temp.Hostname = $hostname.Name
       $temp.Cores = $cores.numberofcores
       $temp.Memory = $memory.TotalPhysicalMemory
       $report += $temp
$report | Export-Csv c:\drscriptosservers.csv –notype

So far the feedback has been very useful and there is some great learning. This is what I've gleaned.;
  • The script is inefficient because it queries WMI more times than it needs to. We can just make one query per WMI class.
  • PowerShell is great because it’s an object-oriented pipeline. The script sends its output to a CSV, which means the output can’t be piped anywhere else. Best practice is to send output to a PowerShell object and let the user do what they want with it. If they want it in a CSV they can pipe it to EXPORT-CSV themselves
  • When we write scripts for other people we should use the full names of the commands and parameters. That just makes the script “self-documenting” and much easier for to read

Event 3 has opened up so we’ll see how that turns out next week.

No comments:

Post a Comment