Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1226
  • Last Modified:

How to force a reboot based on uptime

Hello,

Has anyone found a way to force a laptop or desktop Windows-based computer (XP, 7) to reboot after a period of time.  For example, I would like to implement something that works like this: If the system is up for X amount of days, a message comes up telling the user he needs to reboot based on the policy.  Roughly 66% of our workforce is using laptops and I'm sure a lot of them are simply hibernating nightly instead of rebooting.  This has caused some problems with software we push that require reboots.  Has anyone does anything similar to this?  I'm sure the other option is to disable the hibernate and log off buttons using group policy but I want to try to avoid that at the moment.
0
bryant_buckland
Asked:
bryant_buckland
1 Solution
 
Randy DownsOWNERCommented:
0
 
G3m1n1Commented:
I found the following script already written.  It's VBS using WMI to gather info and perform the reboot if necessary.  The code is commented so you should be able to read through it and modify what you need.

'====================================================================================
'PC/Server Reboot after system uptime equals or exceeds 5 days.
'Written by Robert Johnson, AvantiTech Solutions (c) 2010
'Tested on Windows 2008 Server and Below
'Use Windows task scheduler to schedule this script to run during non-business hours
'====================================================================================

'Establish strings, variables and constants - assign initial values.

'Modify the strContent strings as necessary for custom messages to be writtent to the log file.
strContent0 = "Info: PC reboot log file initialized."
strContent1 = "Info: Checking System Uptime, if equal to 5 days or more, system will reboot."
strContent2 = "Critical: System Uptime is 5 days or greater, this system will be rebooted."
strContent3 = "Info: System Uptime is " 
strContent4 = " days, this system will not be rebooted at this time."
strContent5 = " is being terminated in preparation for rebooting this system."
strContent6 = "This System is being rebooted NOW!"
strFolder = "C:\Scripts\" 'Parent folder for logs folder
strFolder1 = "C:\Scripts\Logs\" 'Logs folder
strFile0 = "C:\Scripts\Logs\PC_Reboot_Logfile.txt" 'Log file.
strDT = Now() 'Returns current system date and time
strProcess = "'hpqtra08.exe'" 'Process to terminate
strSysUpTime = " " 'Initializes variable to an empty value, value will be retrieved by "getSysUpTime" subroutine


'strComputer = InputBox("Enter the Name of the Remote PC: ") 'If you wish to run against a remote computer, then uncomment this line
strComputer = "." 'Run on local computer only | comment out this line if using the above line.

Const FOR_APPENDING = 8 'Constant for writing to log file

Set objFSO = CreateObject("Scripting.FileSystemObject")

CreateLogFile()

Set objTSO = objFSO.OpenTextFile(strFile0, FOR_APPENDING)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

getSysUpTime()

'If system uptime is greater than or equal to 5 days, then reboot the system
'Change this number as necessary for your system(s)
If strSysUpTime >= 5 Then

	killProcess() 'Calls subroutine to kill a process - I had a HP printer process hanging the reboot process. Use if needed.
	
	WScript.Sleep 10000
	
	strDT = Now()
	
	objTSO.WriteLine strDT & " " & strContent2
	
	objTSO.Close()
	
	rebootPC()
Else

	WScript.Sleep 10000
	
	strDT = Now()
	
	objTSO.WriteLine strDT & " " & strContent3 & strSysUpTime & strContent4
	
	objTSO.Close()
	
WScript.Quit

End If

'Retrieves System Uptime from WMI
Sub getSysUpTime()

Set colOperatingSystems = objWMIService.ExecQuery _
     ("Select * from Win32_OperatingSystem") 

For Each objOS in colOperatingSystems
     
     dtmBootup = objOS.LastBootUpTime
     
     dtmLastBootUpTime = WMIDateStringToDate(dtmBootup)
     
     dtmSystemUptime = DateDiff("d", dtmLastBootupTime, strDT)
     
     strSysUpTime = dtmSystemUptime

'     WScript.Echo "System is online since " & dtmSystemUpTime & " days"

Next

End Sub

'Process Kill Routine
Sub killProcess()

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = "& strProcess)

For Each objProcess in colProcessList
    
    WScript.Sleep 10000
    strDT = Now()
    objTSO.WriteLine strDT & " " & strProcess & strContent5
    objTSO.Close()
    objProcess.Terminate()

Next

End Sub

'Reboot subroutine
Sub rebootPC()

Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
    
    WScript.Sleep 10000
    strDT = Now()
    objTSO.WriteLine strDT & " " & strContent6
    objTSO.Close()
    objOperatingSystem.Reboot()

Next

End Sub

'Converts WMI Date String to Date Time format
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

'Creates the folders and files necessary for logging and initializes the log file.
Function CreateLogFile()

	If objFSO.FolderExists(strFolder) = False Then
		
		objFSO.CreateFolder (strFolder)
		objFSO.CreateFolder (strFolder1)
		objFSO.CreateTextFile(strFile0)
		Set objTSO = objFSO.OpenTextFile(strFile0, FOR_APPENDING)
		objTSO.WriteLine strDT & " " & strContent0
		objTSO.Close()
	
	ElseIf objFSO.FolderExists(strFolder1) = False Then
		
		objFSO.CreateFolder (strFolder1)
		objFSO.CreateTextFile(strFile0)
		Set objTSO = objFSO.OpenTextFile(strFile0, FOR_APPENDING)
		objTSO.WriteLine strDT & " " & strContent0
		objTSO.Close()
			
	ElseIf objFSO.FileExists(strFile0) = False Then
		
		objFSO.CreateTextFile(strFile0)
		Set objTSO = objFSO.OpenTextFile(strFile0, FOR_APPENDING)
		objTSO.WriteLine strDT & " " & strContent0
		objTSO.Close()
		
	ElseIf objFSO.FileExists(strFile0) = True Then
	
		Set objTSO = objFSO.OpenTextFile(strFile0, FOR_APPENDING)	
		objTSO.WriteLine strDT & " " & strContent1
		objTSO.Close()
		
	End If

End Function

Open in new window


source: http://community.spiceworks.com/scripts/show/706-reboot-a-system-when-system-uptime-exceeds-x-days

 test-reboot.vbs
0
 
bryant_bucklandAuthor Commented:
That script looks interesting, but I think I see an issue with it.  We have a lot of users who work nights and weekends for system maintenance purposes.  Unless I'm reading it wrong, it looks like the script will reboot the machine if the user is actively logged in.  Have you implemented this in production, Gem1n1?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
G3m1n1Commented:
I noticed that as well.  I think a little modification in the reboot subroutine to generate a pop-up prompt would solve that issue.

I have not implemented this exact script in production because we use group policy, WSUS, and SCCM to enforce reboots after important updates.  My company is all laptops for everyone so we had to come up with a more detailed solution.
0
 
bryant_bucklandAuthor Commented:
Alright, thanks G3m1n1.  I was hoping for more of a supported or Group Policy solution, but I think this will work.  I'll take your script, modify it and make it work for our environment.  Thanks for your help!
0
 
bryant_bucklandAuthor Commented:
I gave it a B only because the solution wouldn't work for us as-is.  It'll require modification of the code to make sure logged in users don't get booted off.
0
 
johnnyt29Commented:
Tried this script and get an error at line 121 about a variable not being set (see first attachment). I comment out the line and run it again to get a message "privilege not held" (2nd attachement). Tried letting task scheduler run it with "highest privilege" but same messages come up. The account I login with does have administrator rights.

What am I missing?
restartscript-err1.jpg
restartscript-err2.jpg
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now