Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need script for restart system after checking last restart time.

Status
Not open for further replies.

shrawanjee1

IS-IT--Management
Jan 26, 2012
1
US
I need script to check the system last restart date and if system has not restart since 7 days then it propmt a message your system has not restarted since 7 days," Yes for Restart and NO for prompt message again after interval of 15 min.
 
Compare with the last boot time, stored in Win32_OperatingSystem.

Code:
'// define vars
strComputer = "."

'// define objects
set objShell = CreateObject("WScript.Shell")
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set tblOS = objWMI.ExecQuery("Select * From Win32_OperatingSystem")

'// traverse tblOS
for each objRow in tblOS
   strBootTime = objRow.LastBootUpTime
next

'// convert to date data type
strDate = cdate(left(strBootTime, 4) & "/" & mid(strBootTime, 5, 2) & "/" & mid(strBootTime, 7, 2))
intDays = datediff("d", strDate, now)

'// main
if (intDays > 7) then 
	do
		intRestart = msgbox ("It's been " & intDays & "since your last reboot.  We you like to reboot now?")
		if (intRestart = vbYes) then
			objShell.Run "shutdown -r -t 0 -f"
		end if
		wscript.sleep 1000 * 60 * 15 'milliseconds
	loop
end if

I would be a little wery of the 15 minutes loop. It might use up resources and may temporarily degrade computer performance.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top