Summary: Microsoft Windows PowerShell MVP, Sean Kearney, provides expert commentary for 2012 Scripting Games Beginner Event 6.
Microsoft Scripting Guy, Ed Wilson, is here. Sean Kearney is the expert commentator for Beginner Event 6.
Sean “The Kraken” Kearney is an infrastructure support analyst, Microsoft MVP in Windows PowerShell, and member of the Springboard Technical Experts Program. He is also one of this year’s first ever Honorary Scripting Guys and the mystery persona behind The BATCHman and Cmdlet. He is presently writing a book about Windows PowerShell.
He is so passionate about Windows PowerShell that he tried naming a pet gerbil “Cmdlet,” and then he attempted to use a Get-Wheel | Invoke-Run on it. We have been advised that he is also barred from any caffeinated beverages when he is presenting due to “The Edmonton Affair” at TechDays Canada.
You’re staring at the desk trying desperately to concentrate and force it to fold into the shape of an Armadillo, when the phone rings breaking your concentration…
“BARK!” a voice barks at you from the other end of the wire. It’s the boss.
“I need to be able pull up metrics on our various servers and have to see how long any one of them has been running. Can you solve this for me? I’ve done some research, and I found that there is information in WMI that could help, but I’m not much of a programmer.”
You smile knowing this is a job Windows PowerShell can solve easily. “Sure! What do you have from WMI?”
“Well there’s a property called win32_operatingsystem that should have the time the computer started up, do you think you could calculate that against the current date somehow?”
“No problem sir….give me a few minutes, and I’ll create a simple script in Windows PowerShell to solve it all.”
You immediately head into the task by firing up Windows PowerShell to see what properties are available from win32_operatingsystem.
GET-WMIOBJECT win32_operatingsystem | GET-MEMBER
Drilling down the list you note that there is one called LastBootupTime. “Aha! I’ll just pull up this property and subtract todays date!” you smile to yourself. Sometimes the boss IS right.
GET-WMIOBJECT win32_operatingsystem | SELECT-OBJECT –expandproperty LastBootupTime
But you stare at the screen with the results with a blank look.
Your eyeballs pop out and roll on the desk. After you pick them up and put them back in your sockets, you take a second look and realize that you are looking at the date. It’s just in a different format. You decide to see if there might be some help from Get-Help about how to parse the date differently.
GET-HELP GET-DATE –examples
Drilling down near example #7, you notice a scenario that almost matches—manipulating the date from win32_bios. It appears that the WMI date is stored differently and there is an available method to convert it to a DotNet format.
You grab the object from win32_operatingsystem and store it away to mimic how the example presented it.
$A= GET-WMIOBJECT win32_operatingsystem
Using the example from Get-Help, you then try to convert the WMI object for LastBootupTime to a normal DotNet date.
You blink and look at the screen in wonder and amazement.
Tuesday, February 01, 2011 5:56:02 AM
“Yes!” You nearly jump out of your shorts, knocking over the gerbil cage. “Now all I need to do is subtract this date from the current date and show the difference! Oh, I can’t wait to have this running on the servers!”
# Get name of local Server
# Get today’s date
# Get the WMI object for the start time
# Obtain the Boot time in DOTNET format
# Calculate how many days the system has been up
# Obtain all the data for the output string
“The computer $Machine has been up for $Days days, $Hours hours, $Minutes minutes, $Seconds seconds, as of $JustDate $JustTime.”
You save the file as GETUPTIME.PS1 and hand it to the boss. He pops onto the security server to test it. He smiles.
“Great job! I knew investing in an IT Pro who knew Windows PowerShell would save me in the long term! Thanks!” And he hands you keys to the most hallowed place in the office…
…the executive coffee room!
2012 Scripting Games Guest Commentator Week Part 2 will continue tomorrow when we will present the scenario for Event 7.
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