How to get system uptime

M

Mark V

In said:
Hello,
how to get system uptime?

One answer: psinfo.exe Free.

Partial output:
---------------------------
C:\TEMP>psinfo

PsInfo 1.36 - local and remote system information viewer
Copyright (C) 2001-2003 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\ABC:
Uptime: 1 day 1 hour 34 minutes 6 seconds

------------
 
P

Phil Robyn

Ondrej said:
Hello,
how to get system uptime?

Ondra.

Or you could look at the timestamp of the file pagefile.sys,
which will show you the date and time that the system was
started.
 
A

Alex K. Angelopoulos [MVP]

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>
 
T

Torgeir Bakken (MVP)

Alex K. Angelopoulos said:
Here's a "pure script" approach - OK, with the usual impurity of a WMI
dependence... <g>

And because of the use of WbemScripting.SWbemDateTime, Win2k3 Server/WinXP only ;-)
 
A

Alex K. Angelopoulos [MVP]

Thanks, Torgeir - forgot to add the "Bugs and Limitations" section...
Added now.
 
N

Ndi

It appears so, even ... My pagefile (only one here too) is create 22th of
October 2002, modified and accessed 15th this month. Uptime is 2 days so I
guess it is "modified" and "accssed" at startup.

Is this guaranteed for all page files? I can find no docs.

I guess it writes at startup as it gets initialised or written the first
time and then an exception is applied to minimize disk overhead.
 
O

Ondøej ©eveèek

Session manager deletes the last pagefile on startup. Then, it creates new
pagefile through NtCreatePageFile() api. Only then it starts CSRSS and
Winlogon.

Ondra.
 
M

Mark V

In said:
Session manager deletes the last pagefile on startup. Then, it
creates new pagefile through NtCreatePageFile() api. Only then it
starts CSRSS and Winlogon.

Is that documented? I believe the pagefile.sys is only initialized on
startup. Not actually "created". This without "clear paging file on
shutdown" set. My Created Date is not the last system restart but
rather the date I last ran PQDI from a floppy (DOS) boot...
 
O

Ondøej ©eveèek

I was to generic. The pagefile is created when not existed, but only
superseded (truncated to 0bytes) when existing. So the actuall file remain
with previous date.

Ondra.
 
M

Mark V

In said:
I was to generic. The pagefile is created when not existed, but
only superseded (truncated to 0bytes) when existing. So the
actuall file remain with previous date.

Okay. That soulds better. :) Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Powershell script to show system uptime 5
Uptime 3
Help Please 1
Pulling more text? 7
System Uptime 4
How can remove the line match before? 13
system uptime 2
Maximum Recommended Uptime 8

Top