Generate Report of Last Boot Time from Computers.txt

email me

This can be used to return Last Boot Time on remote computers. Just add computer names into computers.txt, and run script. A report will be generated, as well as each computer’s info displayed in a pop up. You can disable the pop up if not needed.

VBScript

Option Explicit

Dim objFSO, txtComputers, txtReport, RemoteComputer, objWMI, varBootup
Dim varLastBootTime, varSystemUptime, colOS, objOS, txtCName, txtRName

'computers text file
txtCName = "_Computers.txt"

'report text file
txtRName = "_Boot_Report.txt"


Const ForReading = 1 
Set objFSO = CreateObject("Scripting.FileSystemObject")  
    
Set txtComputers = objFSO.OpenTextFile(txtCName, ForReading) 
Set txtReport = objFSO.CreateTextFile(txtRName) 
Do Until txtComputers.AtEndOfStream  
    RemoteComputer = txtComputers.Readline 
    Set objWMI = GetObject ("winmgmts:\\" & RemoteComputer & "\root\cimv2") 
    Set colOS = objWMI.ExecQuery ("Select * from Win32_OperatingSystem") 
    For Each objOS in colOS 
        varBootup = objOS.LastBootUpTime
        varLastBootTime = DateTranslation(varBootup)     
        'Writes to report
        txtReport.WriteLine "Remote Computer: " & RemoteComputer 
        txtReport.WriteLine "Last Reboot: " & varLastBootTime 
        varSystemUptime = DateDiff("h", varLastBootTime, Now)    
        txtReport.WriteLine "System online since " & varSystemUptime & " hours" & vbCrLf
        'Display pop up
        msgbox RemoteComputer & ": " & varLastBootTime,,"Last Boot"
    Next 

Loop 

'close write to text file
txtComputers.Close 

'done
MsgBox "Done! Report name  " & chr(34) & txtRName & chr(34) & "  was created.",64,"Report"
WScript.Quit(0)

'clear session
set objFSO = Nothing
set txtComputers = Nothing
set txtReport = Nothing
set objWMI = Nothing
set colOS = Nothing
varBootup = ""
RemoteComputer = ""
varLastBootTime = ""
varSystemUptime = "" 
objOS = ""

'translates boot time
Function DateTranslation(varBootup) 
    DateTranslation = CDate(Mid(varBootup, 5, 2) & "/" & Mid(varBootup, 7, 2) & "/" & Left(varBootup, 4) _ 
         & " " & Mid (varBootup, 9, 2) & ":" & Mid(varBootup, 11, 2) & ":" & Mid(varBootup, 13, 2)) 
End Function

 

PowerShell

Clear-Host
 
$txtComputers = "C:\Report\_Computers.txt"
$txtReport = "C:\Report\_Boot_Report.txt"
 
 
#Returns computer names
$computers = Get-Content $txtComputers
 
#Cycles through
foreach ($RemoteComputer in $computers)
    {     
    #Is computer online
    If (Test-Connection -ComputerName $RemoteComputer -ErrorAction SilentlyContinue -Quiet -Count 2) {
        #Returns boot time
        Get-WmiObject Win32_OperatingSystem -ComputerName $RemoteComputer).LastBootUpTime  
        #Convert the last boot time to a reader friendly date time format 
        $time = [System.Management.ManagementDateTimeConverter]::ToDateTime($objWMI)
        #output to console
        $RemoteComputer + "`n" + $time + "`n"
        #output to file
        "$RemoteComputer" + " " + "$time" | Out-file -FilePath $txtReport -Append
    }
    }

	Start-Sleep 5

# Clear session
$txtComputers = ""
$txtReport = ""
$computers = ""
$computer = ""
$objWMI = ""

 

Batch

cls
@Echo off
Title Last Reboot
color 0a
Setlocal EnableDelayedExpansion

:: Report name
Set report=Boot_Report.txt

:: Enter name of computer text file
Set PCList=Computers.txt

cls
Echo Searching for boot times...

:: PROGRAM ROUTINE
:CYCLE
for /f "tokens=* delims= "%%a in (%PCList%) do (
ping %%a | find "Reply" > nul
if errorlevel 1 (
echo %%a>> "_offline.txt"
echo.
%windir%\system32\ping.exe -n 2 127.0.0.1>nul
) else (
echo.
for /f "tokens=1,2,3,4" %%g in ('dir /a:h \\%%a\c$\pagefile.sys ^|find "pagefile.sys"') do (
set bTime=%%a %%g %%h %%i
echo !bTime!
echo !bTime!>>%report%
echo.>>%report%
echo.
)
)
)
pause
endlocal
exit /b 0

:: NOTES
:: you could also script network statistics workstation
:: command: net statistics workstation
:: output: Statistics since 2/21/2017 7:49:38 PM
::
:: or use WMIC for /f %%A in ('WMIC OS GET LASTBOOTUPTIME ^| find "."') DO set DTS=%%A