Ritchie said:
Sneaky.
Here's another 'pure batch' method:-
http://www.commandline.co.uk/lib/Batch Function Library/System Functions/Uptime.html
Here's a "pure script" approach - OK, with the usual impurity of a WMI
dependence... <g>
Save the content below the "=====" line as a file uptime.wsf.
======================================================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<job>
<runtime>
<description><![CDATA[
Name: uptime.wsf
Description
Tells how long the system has been running. Behavior differs significantly
from the POSIX uptime command. With no arguments, simply returns system
uptime in seconds.
To return remote uptimes, simply provide multiple unnamed arguments on the
command line; the returned values will be the uptimes of the named remote
machines in the order given. To include the local system, use either '.' or
the actual name of the system.
To return data in intervals other than seconds, you can use any standard VB
date-format string as an argument to the /I (for interval) switch:
q, m, y, d, h, n, s
Note: to get correct operation from a command prompt using just the stem
name of uptime.wsf, place a CMD or BAT script in the same folder with the
single line
@cscript "%~dpn0.wsf" %*
]]></description>
<example><![CDATA[
Examples
return local uptime in seconds:
uptime
return uptime on remote system SERVER1 in days:
uptime SERVER1 /I:d
return uptime on local host and remote systems FW1 and PC22 in seconds:
uptime FW1 PC22 .
]]></example>
<named name = "?"
helpstring = "Returns this help message"
type = "simple" required = "false"/>
wscript.shell
<named name="I"
helpstring="changes the default interval for the returned time string.
s seconds
n minutes
h hours
d days
m months
q quarters (of year)
y years"
type="string" required="true"/>
<unnamed name="computer"
helpstring="Name(s) of systems to run the command against. Defaults to
local host if none is provided."
many="true" required="false"/>
</runtime>
<script language="VBScript"><![CDATA[
' /i:m /i:h /?
dim COMPUTERS, INTERVAL
Main():WScript.Quit(0)
Sub Main()
ProcessArguments
dim WmiDtm, Computer, WMIService, OperatingSystems, OS, LastBootUpTime
Set WmiDtm = CreateObject("WbemScripting.SWbemDateTime")
For each Computer in COMPUTERS
Set WMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")
Set OperatingSystems = WMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each OS in OperatingSystems
WmiDtm.Value = OS.LastBootUpTime
LastBootUpTime = WmiDtm.GetVarDate
wscript.stdout.writeline DateDiff(INTERVAL, LastBootUpTime, Now)
Next
next
end sub
Sub ProcessArguments()
' General argument processor template routine. Handles
' processing "h" or "help" argument; insert other handlers
' below.
Dim Named
set UnNamed = WScript.Arguments.UnNamed
If UnNamed.Count = 0 then
COMPUTERS = Array(".")
else
set COMPUTERS = UnNamed
end if
If WScript.Arguments.Named.Exists("I") then
Select case lcase(WScript.Arguments.Named.Item("I"))
case "q", "m", "d", "h", "n", "s"
INTERVAL = lcase(WScript.Arguments.Named.Item("I"))
case "y" INTERVAL = "yyyy"
case else
WScript.Quit(1)
end select
Else
INTERVAL = "s"
end if
end sub
]]></script>
</job>