Learn about Windows PowerShell
Summary: Prepare for the Advanced category of the 2013 Scripting Games with this practice event.
Instead of a PowerTip today, we bring you Don Jones from www.powershell.org and the practice event.
This is a practice event. You will not submit this event to the actual Scripting Games, and no official judging will be available for this event. If you would like to post your solution for peer review, please do so in The Scripting Games forum on PowerShell.org or in another venue of your choosing. It is your responsibility to solicit peer reviews from the community; no judges or moderators are scheduled to provide reviews for this practice event.
Dr. Scripto is in a quandary. On one hand, he has a request to calculate the current uptime for a set of servers in his domain. On the other hand, he’d love to go see the new Michael Bay movie. Query uptime, or watch exploding robots?
Easy choice. While Dr. Scripto enjoys a wonderful motion picture, you get to figure out the uptime thing.
You need to write a command that accepts one or more computer names by means of a parameter. That parameter must accept direct input or piped-in string values. Your command must prompt for computer names if none are provided when the command is run.
Your command output must include each computer’s name (even if you were given an IP address, you must display the name) and the number of hours, minutes, and seconds the computer has been online since its last restart. Your output should consist of four properties with hours, minutes, and seconds broken out into their own individual properties.
When your command runs, it should optionally display each computer name or IP address that it is about to connect to.
You can be assured that all of the servers in the list are in the domain, and that you’ll have authority to query them. They’re all running Windows Server 2008 R2 or later, and they all have Windows PowerShell 3.0 installed. There are no firewalls or other blockages between you and those computers, and your command can assume that all of the servers are online and reachable.
However, if an error occurs connecting to a computer, you should display an appropriate error message that includes the failed computer name or IP address. Your command must also provide a switch that causes failed computer names or IP addresses to be logged to a specified text file. If your command is run without that switch, no logging should occur.
If someone so chooses to do so, they should be able to pipe the output of your command to a CSV file, XML file, or HTML file—but you do not need to provide those features in the command you write.
Hint There’s a commonly-used WMI class that contains the most recent computer start-up time. There are also event log entries related to total uptime.
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