Jump to content

[Question] run script before shutdown


laura

Recommended Posts

Hello all

I would like to run script to log session uptime before windows is shutdowning or restarting. The script is something like this:

@echo. >> %systemdrive%\logs\uptime.log
@echo %date:~2,8% %time:~0,8% >> %systemdrive%\logs\uptime.log
@systeminfo | find "Up Time" >> %systemdrive%\logs\uptime.log

Or maybe anybody knows the program which could do this task...

Link to comment
Share on other sites


You could also use a vbscript, there's a lot more code, but it would work much faster!

  • uptime.vbs

Option Explicit
Dim strComputer, strDirectory, strFile
Dim objFSO, objFolder, objTextFile, objFile, objWMIService, objOS
Dim colOperatingSystems, intSystemUptime, TimedAt, M, S, H, D
strComputer="."

'log file location and name
strDirectory="C:\Logs"
strFile="\uptime.log"

'On Error Resume Next
Set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder=objFSO.GetFolder(strDirectory)
Else
Set objFolder=objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory &strFile) Then
Set objFolder=objFSO.GetFolder(strDirectory)
Else
Set objFile=objFSO.CreateTextFile(strDirectory &strFile)
End If
Set objFile=nothing : Set objFolder=nothing
Set objTextFile=objFSO.OpenTextFile(strDirectory &strFile, 8, True)
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)
TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
UpTime(intSystemUptime)
Next

Function UpTime(S)
M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
UpTime=D &" Days, " &H &" Hours, " &M &" Minutes"
objTextFile.WriteLine(TimedAt &", " &UpTime)
End Function

objTextFile.Close
WScript.Quit

I have set lines 8 and 9 to suit your first post but they should be all you would need to change if anything.

The output will all be on one line [date, time without seconds, Uptime (days, hours, minutes)]

  • uptime.log

26/04/2006, 22:47, 0 Days, 13 Hours, 53 Minutes

<Edit>

WMIC script replaced with the VBscript above

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...