Mark Minasi's Reader Forum
Mark Minasi's Reader Forum
Home | Profile | Register | Active Topics | Active Polls | Members | Search | FAQ | Minasi Forum RSS Feed
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old, obsolete or unused
 Scripting Archive
 Script that creates a schedule to in x minutes
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ledson
Major Contributor

USA
1196 Posts
Status: offline

Posted - 06/03/2005 :  12:29:07 PM  Show Profile  Visit ledson's Homepage  Reply with Quote
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

CCIE, CISSP, MCITP:Lync,Exchange,AD, MCSE:Communication (Lync 2013)

jpatton.412
Welcome Newcomer

2 Posts
Status: offline

Posted - 09/11/2008 :  09:13:22 AM  Show Profile  Reply with Quote
Couple questions...
Will this popup a message and give them 5 minutes to close and save anything they are working on?
Does this ask for a username and password? If so can that be removed to it just pops up a 5 minute warning and restarts?
Go to Top of Page

JSCLMEDAVE
Administrator

USA
6133 Posts
Status: online

Posted - 09/11/2008 :  09:33:00 AM  Show Profile  Visit JSCLMEDAVE's Homepage  Click to see JSCLMEDAVE's MSN Messenger address  Reply with Quote
I noticed the original posting date so this may be a non issue. But why not just run shutdown \\IP /r /t 900 in a bat file with the listed IPs?

Tim-

“This too shall pass"
Go to Top of Page

jpatton.412
Welcome Newcomer

2 Posts
Status: offline

Posted - 09/11/2008 :  10:57:26 AM  Show Profile  Reply with Quote
quote:
Originally posted by JSCLMEDAVE

I noticed the original posting date so this may be a non issue. But why not just run shutdown \\IP /r /t 900 in a bat file with the listed IPs?


We have DHCP so they would change, where the static hostname will not. I also want to be able to give them the chance to cancel the reboot if need and export to a logfile that the reboot was cancelled. Any thoughts?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Mark Minasi's Reader Forum © 2002-2011 Mark Minasi Go To Top Of Page
This page was generated in 0.08 seconds. Snitz Forums 2000