Summary: The winner of the advanced division of the 2012 Windows PowerShell Scripting Games gives tips for success.
Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blogger, Rohn Edwards, the winner of the Advanced category in the 2012 Scripting Games. Rohn and Lido Paglia, winner of the Beginner category, will be joining us in Orlando next week. As part of winning the 2012 Scripting Games, they won passes to Microsoft TechEd 2012. Today Rohn is going to talk about lessons that he learned in the 2012 Scripting Games.
Rohn Edwards has been a system administrator since 2006. He primarily works on Windows operating systems. A lot of his work involves automated operating system and software deployment via Microsoft System Center Configuration Manager. He started learning Windows PowerShell about a year ago when he realized that it can do things that are not even possible in VBScript, and he has not looked back since.
Take it away, Rohn...
The 2012 Scripting Games have come and gone, and I’m very happy that I participated in them this year. I am not going to even try to mention every single thing that I picked up, but I do want to talk about a few of the biggest lessons that I learned.
Participation is worth it
I think one of the biggest things I learned is that the Scripting Games are definitely worth entering, no matter your current scripting level. As this year’s games came to a close, one thing that I repeatedly found myself thinking about was how I should have participated last year. At that point, I did not know nearly as much about Windows PowerShell as I did before the games this year. I was able to talk myself into thinking that I would not do very well in the rankings, and between work and family obligations, I wouldn’t have enough time to participate anyway.
After this year’s games, I now know two things about the games that I did not know last year:
- The real reason for participating is to learn, and, even if you think you do not have time to participate, you should try anyway. Regardless of where you place in the final rankings, if you take the time to write and submit entries to at least some of the events, you will gain invaluable practice and experience with one of the most powerful tools the Windows operating systems have ever seen.
- If you truly don’t have time to submit an entry for each event, you haven’t lost out on anything because the games are free to enter.
By not participating last year, I feel like I robbed myself of an incredible learning opportunity. The ten events, along with the expert commentaries and expert judges providing feedback on your scripts, cram months’ worth of scripting experience into just a few weeks’ time. So, if you’re reading this and wondering if the games are worth your time, the answer is an unequivocal, “Yes”!
Comment-based Help is ridiculously simple
Before starting the games, I knew that I could define Help by adding specially formatted comments at the beginning or end of functions. I had not, however, used that feature of Windows PowerShell. Before starting the games, I read the tips that Ed and some of the guest bloggers published on the blog. I kept seeing comment-based Help mentioned, so I assumed that every one of my functions in my entries needed to have it added. I am glad that I did add Help to each of my functions because it helped me realize how incredibly simple it is to do. Everyone knows that good commenting is a must if you want to reuse any of the code that you write. Before the Games this year, I provided a block comment at the beginning of a function or script that gave a brief overview of its purpose, what it took as input, and what it gave as output. That helped when someone was reading the code, but it didn’t provide any way for a user to understand how to use it. Instead of the simple block comment, now I simply provide comment-based Help. If someone comes across the comment in the code, they can read it the same reading it by using Get-Help. If not for the games, I probably would have continued to ignore the comment-based Help system for months.
This may be one of my favorite Windows PowerShell features. A few weeks ago, I would have said that was still the newness of it affecting my judgment, but I still feel the same about it weeks after learning it and using it all the time. What is splatting? It is a way to pass parameters to another function or cmdlet by using a hash table. Here is an example of using Get-Service to get the number of services with a display name that starts with “Microsoft”:
In the previous example, I created a hash table called Parameters with three entries: ComputerName, DisplayName, and ErrorAction. I then “splatted” that hash table to the cmdlet and measured the number of objects returned. Next, I made the same call to Get-Service without splatting. Now, why is the first call better than the second? There are several reasons, but I’ll only list a few:
- It makes your code easier to read, especially if you are passing lots of parameters.
- It makes it easier to make a single call to a function or cmdlet, even when special logic is needed to avoid certain parameters being passed. For example, advanced event 2 required a function that would get service information for a specified computer or set of computers, and you had to support using alternate credentials. Because Get-Service does not support alternate credentials, most entrants chose to use the WMI class Win32_Service. Get-WMIObject (gwmi) supports using alternate credentials, but only when you use them against a remote machine; it doesn’t support alternate credentials on the local computer.
With splatting, you can create a hash table that contains the alternate credentials and any other parameters you want to pass, and before calling gwmi, you can check to see if you are going to run it against the local machine. If you are, you can remove the alternate credentials from the hash table. This lets you make a single call to gwmi inside of your code instead of having logic that will call it with a different set of hard-coded parameters, depending on what you need to do. I did not know about splatting when I wrote the bulk of my entry for Advanced Event 2, so my method was not nearly as elegant as it could have been if I had used it.
- It makes creating wrapper functions very easy. That is actually how I found out about this feature. I had finished my main function for Advanced Event 2, but I wanted a wrapper function that I could create that would call my main function so that the output could be written to a file. I needed a way to forward all the parameters that were sent to my wrapper function to the underlying function that it was calling. I did not want to write code to check for each parameter and send it along if it was present. After a few minutes searching online, I came across a blog post that demonstrated splatting. If you’re wondering, all you have to do to create a wrapper function is make a function that takes the same parameters as the function that you’re wrapping, and then call it and “splat” the auto-generated $PsBoundParameters hash table. If your wrapper function has parameters that the function it is wrapping does not have, all you have to do is make a copy of $PsBoundParameters and remove the unneeded parameters from the hash table.
This one blew my mind. I did not learn this myself while writing for any of the events. Expert Commentary: 2012 Scripting Games Advanced Event 2 by Boe Prox demoed this. His commentary pointed to a webcast by Dr. Tobias Weltner, Speeding up Windows PowerShell: Multithreading. So what is it, and why do I like it so much? Multithreading within Windows PowerShell gives you the ability to, among other things, run a command locally on your machine against lots of remote machines in a fraction of the time it would take to run that same command against the machines one at a time.
As long as you take thread safety into account, you should be able to use this feature to speed up any data parsing/processing that requires reading several files. PSJobs can do this to some extent, but RunSpacePools seem to offer more power, speed, and flexibility. The one area where I think PSJobs might beat RunSpacePools is simplicity. As long as you do not try to implement a limit to the number of threads you can run concurrently, it is much easier to follow what is going on when using PSJobs. After checking out the examples by Dr. Tobias Weltner and Boe Prox, and understanding what is going on, I think that RunSpacePools has earned a special place in my arsenal of Windows PowerShell tools.
So, was participating in the 2012 Scripting Games worth it? Yes! Would I still be glad that I participated if I did not win any prizes? Absolutely! I would recommend participating in the Scripting Games to anyone, whether they are completely new or an advanced pro when it comes to Windows PowerShell. The Games provide a chance to learn and enforce invaluable new and existing Windows PowerShell knowledge. The daily prize drawings and the grand prizes are just icing on the cake!
Thanks, Rohn! I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy