Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Uptime program?

Posted on 2008-10-25
6
Medium Priority
?
341 Views
Last Modified: 2012-06-21
Hi experts,

I wish to log my hours worked on my PC at work.
Ive attached an uptime script i want to run at shutdown to write session uptime to a file. Is it possible to modify this script so it updates the outputed text file by adding the previous uptime to it giving me my full uptime by the end of the week?

best rgds,
Grace



Dim filesys, logtimes 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Set  logtimes= filesys.CreateTextFile("c:\Documents and Settings\grace\Desktop\uptime.txt") 
 
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")
 
For Each objOS in colOperatingSystems
intSystemUptime = Int(objOS.SystemUpTime)
logtimes.Write intSystemUptime & " seconds"
Next

Open in new window

0
Comment
Question by:grace101
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 22805456
Shows how to append instead of overwrite, and also add a date/time stamp for each line.
Dim filesys, logtimes 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Const fsoForAppend = 8
Set  logtimes= filesys.OpenTextFile("c:\Documents and Settings\grace\Desktop\uptime.txt", fsoForAppend, True) 
 
 
Dim strComputer
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")
 
For Each objOS in colOperatingSystems
    intSystemUptime = Int(objOS.SystemUpTime)
    logtimes.WriteLine intSystemUptime & " seconds at " & Now()
Next
logtimes.close
Set logtimes = Nothing
Set filesys = Nothing

Open in new window

0
 

Author Comment

by:grace101
ID: 22806673
Thanks for your response.
It keeps logging uptime even when my laptop goes into sleep mode. Is there a way around this?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 22807779
Yeah, I could see that would be a problem.

I can think of a way to do this in .NET but I can't think of a robust way to handle it with VBScript.  In .NET it's possible to capture the events for sleep mode.

About the best I could think of is to have two scripts.  One that you run when you startup and one that you run when you shut down.

'Startup.vbs
Dim filesys, logtimes 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Const fsoForAppend = 8
Set  logtimes= filesys.OpenTextFile("c:\Documents and Settings\grace\Desktop\uptime.txt", fsoForAppend, True) 
 
 
 
logtimes.WriteLine "Startup " & Now()
 
logtimes.close
Set logtimes = Nothing
Set filesys = Nothing
 
'Shutdown.vbs
Dim filesys, readtime, logtime
Set filesys = CreateObject("Scripting.FileSystemObject") 
Const fsoForAppend = 8
Const fsoForReading = 1 
 
Set  readtime= filesys.OpenTextFile("c:\Documents and Settings\grace\Desktop\uptime.txt", fsoForReading, True) 
 
 
 
Dim strLine
Dim dtStartup 
 
Do While readtime.AtEndOfStream = False
    'Find last line in file
    strLine = readtime.ReadLine
Loop
readtime.Close
Set readtime = Nothing
 
If Not strLine = "" Then
    If Instr(strLine, "Startup ") Then
        dtStartup = CDate(Mid(strLine, 9))
    End If
End If
 
If IsEmpty(dtStartup) Then  'Check if last line contained a startup time
    Msgbox("Startup Time not found in file.  Check if the last line in the file is a start up time")
Else
    Set logtime = filesys.OpenTextFile("c:\Documents and Settings\grace\Desktop\uptime.txt", fsoForAppend, True) 
    logtime.WriteLine DateDiff("s", dtStartup, Now()) & " at " & now
    logtime.Close
    Set logtime = Nothing
    
End If
 
Set filesys = Nothing

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:grace101
ID: 22808376
I found 2 programs that might be able to help me. One of them is copied in a snippet below. AwakeScript.vbs just appends "Out of standby" with a timestamp  to uptime.txt. (It doesnt actually do it yet.. im getting an error(but ive posted it on another post so should have it right soon).




The other one for going into standby is as follows (unmodified from the way i found it)


Option Explicit

Dim wmiPowerManagementEvent, objProcess
Const cntEventEnteringSuspend = 4

Set wmiPowerManagementEvent = GetObject("winmgmts:").ExecNotificationQuery("Select * from Win32_PowerManagementEvent")
Do
   If wmiPowerManagementEvent.NextEvent.EventType = cntEventEnteringSuspend Then
      For Each objProcess in GetObject("winmgmts:").ExecQuery("select * from Win32_Process where name='ehshell.exe'")
        objProcess.Terminate(0)
        Next
   End If
Loop


Is it possible to make this second script run "Sleepnow.vbs" ( which appends "Into Standby" plus a timestamp to uptime.vbs instead of terminating whatever its doing?

PS Im able to manage Sleepnow.vbs:-)




Option Explicit
 
Dim wmiPowerManagementEvent, oShell
Const cntEventEnteringWakeup = 7
 
Set wmiPowerManagementEvent = GetObject("winmgmts:").ExecNotificationQuery("Select * from Win32_PowerManagementEvent")
Do
   If wmiPowerManagementEvent.NextEvent.EventType = cntEventEnteringWakeup Then
 set oShell=createobject("WScript.Shell")
 oShell.Run "C:\Documents and Settings\Grace\Desktop\AwakeScript.vbs"
   End If
Loop

Open in new window

0
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 22809516
You can run one script from another like this:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\Sleepnow.vbs", 1, True  '1 shows the window, and true waits until it's fiinished to return

http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
0
 

Author Closing Comment

by:grace101
ID: 31509920
Thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question