简体   繁体   English

用于检查自上次在 Windows 10 上重新启动以来的天数的脚本

[英]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.下面的 VB 脚本用于获取自上次在 Windows 10 设备上重新启动以来的天数。 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.目的是将脚本作为计划任务运行,如果天数小于 13,那么它将退出 0 且不执行任何操作。 If the number of days is higher than 13 then exit 1. The script works fine on many devices.如果天数高于 13 则退出 1。该脚本在许多设备上都可以正常工作。 But on some devices it's showing negative value for the number of days.但在某些设备上,天数显示为负值。 Any suggestions to overcome the issue.任何解决问题的建议。

PC with Issue有问题的电脑

TIA 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

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.该脚本使用 mm/dd/yyyy 但从本地计算机继承其区域设置。 If the local machine uses dd/mm/yyyy format, then the script will return an incorrect result.如果本地机器使用 dd/mm/yyyy 格式,那么脚本会返回错误的结果。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM