Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Question] run script before shutdown

Featured Replies

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

Start > Run > gpedit.msc

Computer Configuration > Windows Settings > Scripts > Shutdown

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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.