Here's a script I created a while back to restart all machines in an input file in 15 minutes. (For a network migration)
It copies two vbscripts on the machine, a restart one, & a clean job one. (so it doesn't show up in the scheduled tasks after it ran) - adds the "clean job" script to the RunOnce registry key, so that it cleans the schedule after it restarts, (can't have the machine restarting every day at that time) & outputs script job success level to a log file, so you know which PCs are going to restart or not.
Enjoy!
'============================================================================
'
' NAME: CreateSchedSen.vbs
'
' AUTHOR: Luke Edson, Edsontech
' DATE : 11/08/2004
'
' COMMENT: VBScript to create schedule to restart remote machine when needed.
'
' IMPORTANT: This script creates a schedule that will run in 15 minutes.
'
' Copyright (c) 2004 Luke P. Edson - ledson@edsontech.com
' Latest Version 1.6 - December 4, 2004
' Original Version 1.0 - November 8, 2004
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided
' that you agree that the copyright owner above has no warranty,
' obligations, or liability for such use.
'
'============================================================================
sInputFile = "c:\hosts.txt" 'source file.
sOutputFile = "c:\SchedHosts.log" 'output log file
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const HKEY_LOCAL_MACHINE = &H80000002
Const INTERVAL = "n"
Const MINUTES = 15
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objFS = CreateObject("Scripting.FileSystemObject")
strUserID = InputBox("Enter the NTDomain\User for the Restart schedule:","Enter Domain Username","NTDomainName\Username")
strPassword = InputBox("Enter the Password for the Restart schedule:","Enter Domain Password")
strPassword1 = InputBox("Re-Enter the Password for the Restart schedule:","Re-Enter Domain Password")
If strPassword <> strPassword1 Then
Dummy = WshShell.Popup("Passwords do not match, please try again!",0, "Oops, Different Passwords",16)
WScript.Quit
End If
If objFS.FileExists(sOutputFile) Then
Set OutFile = objFS.OpenTextFile(sOutputFile, ForAppending, True)
Else
Set OutFile = objFS.OpenTextFile(SOutputFile, ForWriting, True)
End If
' Insert Time and Date Stamp upon running
OutFile.WriteBlankLines(1)
OutFile.WriteLine "---------------------------------------------"
OutFile.WriteLine "Processing Started - " & Time & " " & Date
OutFile.WriteLine "---------------------------------------------"
OutFile.WriteBlankLines(1)
' Open the input file
Set inFile = objFS.OpenTextFile(sInputFile, ForReading, True)
While Not inFile.AtEndOfStream
Dim objWMIService
arrComputers = Split(inFile.Readline, vbTab, -1, 1)
strComputer = arrComputers(0)
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2", strUserID, strPassword)
objWMIService.Security_.ImpersonationLevel = 3
If Err.Number <> 0 Then
OutFile.writeline Now & vbTab & "Error connecting to " & strComputer & " --- " & Err.Description
Err.Clear
Else
' deploy scripts to client PC
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "X:", "\\" & strComputer & "\C$"
objFS.getfile "C:\Restart.vbs"
objFS.CopyFile "C:\Restart.vbs", "X:\", True
objFS.getfile "C:\CleanSchedJob.vbs"
objFS.CopyFile "C:\CleanSchedJob.vbs", "X:\", True
If Err.Number = 0 Then
OutFile.WriteLine "Script files copied to:" & VbTab & VbTab & strComputer & " at " & Time
Err.Clear
Else
OutFile.WriteLine "Script files NOT copied to:" & VbTab & strComputer & " at " & Time
Err.Clear
End If
objNetwork.RemoveNetworkDrive "X:"
' set date & time for schedule
intDate = Weekday(Date) - 2
If intDate < 0 Then
intDate = 6
End If
strDate = 2 ^ intDate
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
strTime = Right(objSWbemDateTime.Value, 17)
' create schedule to invoke the restart script
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
strErrNum=objNewJob.Create("c:\Restart.vbs", "********" & strTime, True, strDate, , , JobID)
If strErrNum = 0 Then
OutFile.WriteLine "Schedule job created on:" & VbTab & strComputer & " at " & Time
OutFile.WriteBlankLines(1)
Err.Clear
Else
OutFile.WriteLine strErrNum
OutFile.WriteLine "Schedule job NOT created on:" & VbTab & strComputer & " at " & Time
OutFile.WriteBlankLines(1)
Err.Clear
End If
' Add CleanSchedJob.vbs to the registry to delete the schedule after reboot
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
strValueName = "CleanSchedJob"
strValue = "C:\CleanSchedJob.vbs"
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
End If
Wend
OutFile.WriteLine "============================================="
OutFile.WriteLine "Processing Stopped - " & Time & " " & Date
OutFile.WriteLine "============================================="
inFile.close
Dummy1 = WshShell.Popup("The schedule script has completed, please check the log file.",0, "Schedule Script Complete",64)
Luke Edson