Learn about Windows PowerShell
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.
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:
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”!
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:
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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Great stuff here that everyone can benefit from. Thanks also for the shout-out as well! I wrote an article a little while back that shows some of the performance differences with using runspaces vs. PSJobs here: learn-powershell.net/.../using-background-runspaces-instead-of-psjobs-for-better-performance
Congrats again on winning this year's Advanced division!
@Boe Prox Yes, Rohn did a great job -- both in the Scripting Games, and in today's blog posting. I am really looking forward to getting to hang out with him at the Scripting Guys booth at TechEd in Orlando. The Scripting Wife and I are also looking forward to getting to hang out with you as well! WOO HOO it will be fun.