Home » KBArticles » SCRIPT: query server uptime output to text file

SCRIPT: query server uptime output to text file

‘vbscript
‘list computers to check in com,puters.txt in same directory script is run from
‘will write server uptime to uptime.txt in same directory
rfile = “computers.txt”
wfile = “Uptime.txt”

on error resume next
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set r = objFSO.OpenTextFile(rfile, ForReading, True)
Set w = objFSO.OpenTextFile(wfile, ForWriting, True)
arycomp = split(r.readall, vbcrlf)
r.close

for each pc in arycomp
Set objWMIService = GetObject(“winmgmts:\” & pc & “rootcimv2”)
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Set colOperatingSystems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff(“d”, dtmLastBootUpTime, Now)
‘how many days since the last reboot.
‘w.writeline “SYSTEM UPTIME”
w.writeline pc & vbtab & “Up Time (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
‘WScript.Echo pc & vbtab & “UpTime (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Next
objWMIService = Nothing
colOperatingSystems = Nothing
next
w.writeline “EOF”
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))
‘uncomment the belowline in order to see the actual time and date the computer was last restarted.
‘wscript.echo WMIDateStringToDate
End Function

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.