Skip to content

Instantly share code, notes, and snippets.

@malcom
Created September 9, 2009 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcom/183755 to your computer and use it in GitHub Desktop.
Save malcom/183755 to your computer and use it in GitHub Desktop.
uptime tool for Windows
# uptime tool for Windows
# Copyright (c) 2009 Marcin 'Malcom' Malich <me@malcom.pl>
# License MIT
# http://projects.malcom.pl/tools/uptime.xhtml
# http://blog.malcom.pl/2009/07/09/skryptowanie-windowsa-perl/
use strict;
use Win32::OLE qw(in);
use Win32::OLE::Variant;
use Time::Local;
my $strComputer = '.';
my $objWMIService = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
foreach my $objOS (in $objWMIService->InstancesOf('Win32_OperatingSystem')) {
my $objSWbemDateTime = Win32::OLE->new('WbemScripting.SWbemDateTime');
$objSWbemDateTime->{Value} = $objOS->LastBootUpTime;
my $bootTime = timelocal($objSWbemDateTime->Seconds, $objSWbemDateTime->Minutes,
$objSWbemDateTime->Hours, $objSWbemDateTime->Day,
$objSWbemDateTime->Month - 1, $objSWbemDateTime->Year);
my $nowTime = timelocal((localtime)[0..5]);
my $difference = $nowTime - $bootTime;
my ($seconds, $minutes, $hours, $days);
$seconds = $difference % 60;
$difference = ($difference - $seconds) / 60;
$minutes = $difference % 60;
$difference = ($difference - $minutes) / 60;
$hours = $difference % 24;
$difference = ($difference - $hours) / 24;
$days = $difference;
my $strUptime = "$hours:$minutes:$seconds";
if ($days == 1) { $strUptime = "$days day, $strUptime"; }
elsif ($days > 0) { $strUptime = "$days days, $strUptime"; }
print 'Uptime: ' . $strUptime;
}
# uptime tool for Windows
# Copyright (c) 2009 Marcin 'Malcom' Malich <me@malcom.pl>
# License MIT
# http://projects.malcom.pl/tools/uptime.xhtml
# http://blog.malcom.pl/2009/06/07/skryptowanie-windowsa-wsh/
Option Explicit
Const strComputer = "."
Dim objWMIService, objOS
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
For Each objOS In objWMIService.InstancesOf("Win32_OperatingSystem")
Dim objSWbemDateTime
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.Value = objOS.LastBootUpTime
Dim seconds, hours, minutes, days
seconds = DateDiff("s", objSWbemDateTime.GetVarDate, Now)
days = Fix(seconds / 86400)
seconds = seconds - (days * 86400)
hours = Fix(seconds / 3600)
seconds = seconds - (hours * 3600)
minutes = Fix(seconds / 60)
seconds = seconds - (minutes * 60)
Dim strUptime
strUptime = hours & ":" & minutes & ":" & seconds
if (days = 1) then
strUptime = days & " day, " & strUptime
elseif (days > 0) then
strUptime = days & " days, " & strUptime
end if
WScript.Echo "Uptime: " & strUptime
next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment