Search code examples
windowsvbscript

Script to check number of days since last reboot on Windows 10


Below VB script is written to get the number of days since last reboot on Windows 10 Devices. The aim is to run the script as a scheduled task and if the number of days is less than 13 then it'll exit 0 with no action. If the number of days is higher than 13 then exit 1. The script works fine on many devices. But on some devices it's showing negative value for the number of days. Any suggestions to overcome the issue.

PC with Issue

TIA

    ON ERROR RESUME NEXT

'Set Variables
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objfso = CreateObject("Scripting.FileSystemObject")
Set wshell = CreateObject("WScript.Shell")


strComputer = "."
str_folder = "C:\Temp\LogFolder"
str_logfile = str_folder & "\Logfile.log"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("n", dtmLastBootUpTime, Now)
    numUptDays = (dtmSystemUptime \ 60 ) \ 24
Next

Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
        Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
            & " " & Mid (dtmBootup, 9, 2) & ":" & _
                Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
End Function

If numUptDays > 13 Then
'Create Folder
If not objfso.FolderExists(str_folder) Then 
    objfso.CreateFolder str_folder
End If

'Create Log File
If not objfso.FileExists(str_logfile) Then 
    Set objFile = objFSO.CreateTextFile(str_logfile)
    objFile.Close
End If

'Update Log File - Rebooting
str_text = "Restart Required"
UpdateLog(Now & " -- " & str_text)
'quit and set exit code
wscript.quit(1)

Else

'Create Folder
If not objfso.FolderExists(str_folder) Then 
    objfso.CreateFolder str_folder
End If


'Create Log File
If not objfso.FileExists(str_logfile) Then 
    Set objFile = objFSO.CreateTextFile(str_logfile)
    objFile.Close
End If


'Update Log File - Reboot not Required
str_text = " days since last reboot. No reboot required."
UpdateLog(Now & " -- " & numUptDays & str_text)
'quit and set exit code
wscript.quit(0)
End If

'Function to Update LogFile
 Function UpdateLog(str_text)
    Set objFile = objfso.OpenTextFile(str_logfile, ForAppending, TristateFalse)
    objFile.Write str_text & vbcrlf
    objFile.Close
 End Function

Solution

  • The script is not working on some devices due to a date format difference. The script uses mm/dd/yyyy but inherits its locale setting from the local machine. If the local machine uses dd/mm/yyyy format, then the script will return an incorrect result.

    To ensure that the script runs the same, regardless of the machine's locale, the script must have an explicitly set locale. This is best set somewhere near the top of the script. In this case, the line that needs to be added is:

    Setlocale("en-us")