Learn about Windows PowerShell
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 $Machine=Hostname
# Get today’s date $Today=GET-DATE
# Get the WMI object for the start time $A=GET-WMIOBJECT win32_operatingsystem
# Obtain the Boot time in DOTNET format $BootTime=$A.ConverttoDateTime($A.LastBootupTime)
# Calculate how many days the system has been up $Uptime=$Today-$Boottime
# Obtain all the data for the output string $Days=$Uptime.Days $Hours=$Uptime.Hours $Minutes=$Uptime.Minutes $Seconds=$Uptime.Seconds $JustDate=$Today.toshortdatestring() $JustTime=$Today.toshorttimestring()
“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
I sincerely hope the kraken looks like that on casual friday's
Great info... I still haven't figured out exactly what the -expandproperty parameter of select-object is used for... read some help on it, and still confused. I'm going to continue reading, and testing examples out... thanks again for the info.
Oh wait, I think I just figured out - testing it with the example in the above blog post gives me the answer...lol... If you didn't use -expandproperty, you would get the property name in the results as well... with -expandproperty, you only get the data in the column (Hope I said it right - please feel free to correct it) :)
Another good use of the -ExpandProperty parameter is when one of the properties contains an array of items...
Instead of getting the "array", you get a list of each item in the array.
For instance, if I did:
Get-Service winrm | select ServicesDependedOn
I get the following output:
BUT if I did:
Get-Service winrm | select -ExpandProperty ServicesDependedOn
I get this output, which can be much more helpful depending on what I wanted to find out:
Status Name DisplayName
------ ---- -----------
Running HTTP HTTP
Running RPCSS Remote Procedure Call (RPC)
Powershell is so versatile :)
Awesome! Thank you very much for that information/example! I have totally fallen in love with Powershell - we use it here at work pretty heavily, as well as me at home (learning).
@socaldavis My love affair with Powershell has gotten me into a LOT of trouble. It truly is a tool we fall in love with because we need so little to make it useful.
@plindgren I tried to sneak that into a casual Friday, but I'm pretty certain somebody would have countered that with a pink slip ;)
@dawn Great Example of using ExpandProperty. Really when you get down to it, all it does it pull the information out (all we want) and give it to us without a descriptive field.
If anybody sees a small Gerbil running about singing "Highway to Powershell", tell him his music contract is still up for negotiation :)