System Up Time

The PowerShell team have just posted about obtaining system up time using a .NET class to convert the WMI date format to something thats readable

http://blogs.msdn.com/powershell/archive/2009/08/12/get-systemuptime-and-working-with-the-wmi-date-format.aspx

There is a WMI only way of doing this

$os = Get-WmiObject -Class Win32_OperatingSystem
$os.ConvertToDateTime($os.LastBootUpTime)

But this gives the date and time the system was last booted.  If you want the actual up time then use

(get-date) - $os.ConvertToDateTime($os.LastBootUpTime)

which generates a timespan object

Days              : 0
Hours             : 1
Minutes           : 53
Seconds           : 8
Milliseconds      : 461
Ticks             : 67884618000
TotalDays         : 0.0785701597222222
TotalHours        : 1.88568383333333
TotalMinutes      : 113.14103
TotalSeconds      : 6788.4618
TotalMilliseconds : 6788461.8

which gives the actual up time.  The .NET method can be substituted for the WMI method if required

Technorati Tags: ,,
Published Thu, Aug 13 2009 0:11 by RichardSiddaway
Filed under:

Comments

# re: System Up Time

ONELINER

(get-date)  - (Get-WmiObject Win32_OperatingSystem ).ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)

but pretty ugly, and two WMI requests.

Thursday, August 13, 2009 6:01 AM by pan_2@LJ

# re: System Up Time

with .net its better looking

(get-date)  - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)

Thursday, August 13, 2009 6:04 AM by pan_2@LJ

# re: System Up Time

The one liner is ugly and doesn't gain anything.  Probably slower due to two WMI calls

.NET version is better looking but looks don't win when scripting. As long as it work ugly is good

.NET vs WMI probably comes down to personal preference

Thursday, August 13, 2009 2:43 PM by RichardSiddaway

Leave a Comment

(required) 
(required) 
(optional)
(required)