Friday, May 24, 2013

What I Learned at the Scripting Games - Part 3

Well the number one thing I got out of this week was that we need to raise our domain functional level to server 2008R2 (at a minimum).

I spent quite a while getting the script to work with server 2003 domain controllers. A point that seemed lost on the crowd.

There are some neat tricks in this script though and I learned a lot because I chose to make it harder on myself. Casting Types, BitWise checks, "filetime" oh and most importantly the CSS for pink mouse hovering!  Credit there must go to my boss Nathan. Well for the mouse over bit... The pink was all me!

If server 2008 was going to be the target platform we could have used something like this;

Get-ADUser | Get-Random -Count 20 | select all the stuff | ConvertTo-Html Blah blah

But I had fun none the less. The scripting games has been really fun and informative. It's really great seeing how different scripters will attack the same problem. Bring on Problem 5!

Also I should being planning that domain functional level upgrade... There's a lot of powershell goodness to be leveraged!

Friday, May 17, 2013

What I Learned at the Scripting Games - Part 2

Script for event 3 has been a hit (so far)! It’s crowd score is tracking above 4 stars for now. That's a big improvement from last week!

This was the problem 

The positive reception seems to be mainly linked to the time spent making the output look pretty with some CSS. Setting the formatting so the script is super easy to read doesn't hurt either.

Script is here as an image because when you include HTML tags the blogger platform gets all huffy.

Someone pointed out that it could be more efficient. I’m doing;
Get-WmiObject win32_logicaldisk -computername $server | Where-Object {$_.DriveType -eq 3} |

Where it could be;
Get-WmiObject win32_logicaldisk -computername $server –filter=”Drivetype=3” |

Mine executes two commands (get-wmiobject and where-object) when the same result could be achieved with one. If this script was running over tens of thousands of hosts it might make a difference. Plus it’s neater with one command.

So lessons learned this week;
  • Make sure you are being as efficient as possible even if your script is only going to be run on one host today... That will almost certainly not be the case tomorrow
  • Spending a bit of effort making the output "pretty" is worth the effort
  • Spending time to make your script readable will help others comprehend what you are doing. It'll also help future you figure out what you were thinking
  • There may still a place for oddball humor in comments! 

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.