Wednesday, 18 July 2012

LttP: Powershell Games 2012 - Beginner Event 6

Well, Beginner Event 6 looks quite familiar.  I've had to do exactly this before, albeit with a different output.  Anyway my "entry":

[datetime]$LastBootTime = (Get-WmiObject Win32_OperatingSystem).ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)
$UpTime = New-TimeSpan -Start $LastBootTime -End (Get-Date)

$Message="The computer "
$Message+=$env:COMPUTERNAME
$Message+=" has been up for "
$Message+=$UpTime.Days
$Message+=" days "
$Message+=$UpTime.Hours
$Message+=" hours "
$Message+=$UpTime.Minutes
$Message+=" minutes, "
$Message+=$UpTime.Seconds
$Message+=" seconds as of "
$Message+=(Get-Date).ToString()

Write-Host $Message

1 comment:

Mike F Robbins said...

“The only real guidance he provided for the task is, Use WMI.”

You can get everything you need from WMI without having to use $Env:Computername or Get-Date for anything.

Here's the script I submitted for this one:
http://2012sg.poshcode.org/4514

And the blog article I wrote about it:
http://mikefrobbins.com/2012/04/18/working-with-wmi-2012-powershell-scripting-games-beginner-event-6/

Mike F Robbins
http://mikefrobbins.com