Learn about Windows PowerShell
Summary: In Beginner Event 6, you are required to compute the uptime for the local computer.
About this event
Date of Event
4/9/2012 12:01 AM
4/16/2012 12:01 AM
You are the main network administrator for a small company. As such, your duties consist of a variety of functions, including assisting the Help Desk when they get behind. Recently, your manager has become concerned about server uptime. He wants you to write a script that will display how long a server has been “up.” He said that he is not concerned with anything fancy—he just wants a general idea. The only real guidance he provided for the task is, “Use WMI. There is a class called Win32_OperatingSystem that should do the trick for you. Tell me the server name, and how many days, hours, and minutes the server has been up.”
The output that is shown in the image that follows meets the boss’s requirements.
2012 Scripting Games: All Links on One Page
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. Good luck as you compete in this year’s Scripting Games. We wish you well.
Ed Wilson, Microsoft Scripting Guy
This one wasn't too difficult. 10 seconds after I submitted my script, I realized I should have done something different that might cause me a point. I guess in the future I will make sure I am super happy with my final script before submitting. Double check everything, and make sure you are completely happy with your scripts.
I'm really struggling on this one. Think I've found the property I need to use but it's a string and I can't figure out how to make it more friendly and able to go into the equation I want it to. Any advice?
@Chris-D This is good advice, double check everything and make sure you are happy with your script. I would also add that you have 7 days before an event becomes due, and so you can set it aside for a little bit. Also be careful when submitting the script -- make sure you are submitting it for the correct event!
@Mr Killian Wow ... hmmmm... let me see. Use Get-Member and look at both the properties and the methods. You might also want to look at the Hey Scripting Guy Blog to see about working with WMI dates. There may be some links in the study guides for the Scripting Games. I have study guides on the All in One page for several years of Scripting Games. There may be a section about working with dates there.
I am kicking myself, I had it all done and just noticed something is amiss. I didnt see it since I normally dont have it set to this value.
Just to confirm, the boss asks for minutes but the script output shows seconds. Which should we be using?
@Matt Tilford Yes, you are right the boss only asked for minutes, but the output also includes seconds. It is not a requirement, but I threw it in there because I already had the value. You would not lose a point for including extra information -- but you would lose points if your solution does not include the minutes.
Wow went off on a tangent on this one and it was getting really complicated before I changed direction and found a much easier way to do this!
The design points don't mention actually using the win32_operatingsystem. If there's a way around it, can we use that or is it one of the requirements of the script?
@Jlsuperman the boss specified that you should use win32_operatingsystem, so that makes it a requiement.
I think I've figured this one out but after getting 1 star on one that I thought I had nailed I think I'll sleep on it and check again tomorrow. Triple check is my new mantra.
I see my Get-Date is returning 24-hour time when converted to string (no AM/PM). Would I lose a point for not formating to the 12 hour time with AM/PM per your sample output? You mention paying attention to the formatting of the output, but it seems to me it would add unneeded work to format the date for 12hour AM/PM.
@Daniel-D 24 hour format will be fine. I do not expect you to calculate AM / PM.
By "Up" time, do you mean how long has it currently been running? Or do you mean how long has it been "Up" for the life of the server? I am guessing by the sample output you just mean currently running, but thought I should double check.
Quick clarifying question : By uptime you mean how much time the server has been up since it rebooted? Or how much time in general?