none
Need a time schedule in vbs script to execute something

    Question

  • Hi every one.

    I need to execute a batch file in a specifyed time for all users in active directory.

    I know i can put a vb script on the "Logon Script" for all users to run the batch file.

    But i can't write the vbs script for doing this.

    can any one help me please?

    Saturday, July 17, 2010 11:02 AM

Answers

  • Hi

    I'd imagine something like this should do the trick...just change the variable "filespec" to the location of the batch file you want to run and set the time in the TimeSerial function. For example "TimeSerial(15, 00, 00)" = 3PM.

    Do you want to run this batch file every day or once only for all users? This will exit after the batch file has run at the specified time. Here is the code. Hope that helps

    Cheers Matt :)

    '---------------------------------------------------------------------------------------------------------------------------- 
    'Script Name : RunTask.vbs   
    'Author   : Matthew Beattie   
    'Created   : 17/07/10
    'Description : This script runs as a backgroud process in an infinate loop. It wakes up every 10 minutes to determine if 
    '      : the current time is 3PM on a weekday. If it is it attempts to execute a local batch file otherwise it will 
    '      : wait 590 seconds (almost 10 minutes) to allow the system 10 seconds to wake up if the system is busy 
    '      : processing.
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Initialization Section   
    '---------------------------------------------------------------------------------------------------------------------------- 
    Option Explicit
    Dim objFSO, wshShell
    On Error Resume Next  
      Set objFSO  = CreateObject("Scripting.FileSystemObject")
      Set wshShell = CreateObject("Wscript.Shell")
      ProcessScript
      If Err.Number <> 0 Then   
       Wscript.Quit   
      End If   
    On Error Goto 0   
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Functions Processing Section   
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ProcessScript -> Primary Function that controls all other script processing.
    'Parameters : None     -> 
    'Return   : None     -> 
    '----------------------------------------------------------------------------------------------------------------------------
    Function ProcessScript
      Dim scheduledTime, fileSpec, waited, i
      fileSpec = "C:\Scripts\RunTask.bat"
      '-------------------------------------------------------------------------------------------------------------------------
      'Run the script as a background process in an infinate loop. Use task manager to terminate "wscript.exe" to cancel it.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       '----------------------------------------------------------------------------------------------------------------------
       'Determine the exact time to run the process the Calculate the number of seconds waited.
       '----------------------------------------------------------------------------------------------------------------------
       scheduledTime = Int(Now) + TimeSerial(15, 00, 00)
       waited    = DateDiff("S", Now, scheduledTime)
       '----------------------------------------------------------------------------------------------------------------------
       'Run the process if it's time to execute the batch file otherwise continue waiting
       '----------------------------------------------------------------------------------------------------------------------
       On Error Resume Next
         If waited <= 600 Then
          Wscript.Sleep waited * 1000
          '----------------------------------------------------------------------------------------------------------------
          'Ensure the Batch file exists. Terminate the script from running in an infinate loop if it doesn't exist.
          '----------------------------------------------------------------------------------------------------------------
          If objFSO.FileExists(fileSpec) Then
            wshShell.Run "cmd /c " & fileSpec, ,False
            If Err.Number <> 0 Then
             WScript.Quit
            End If
            Exit Function
          End If
         Else
          WScript.Sleep 590 * 1000
         End If
       On Error Goto 0
      Loop
    End Function
    Saturday, July 17, 2010 12:20 PM
    Moderator
  • Hi

    I tested it on XP SP3 (WScript 5.7) where the content of the batch file just executes  a simple "ping localhost". It Works fine for me and opens a command prompt to execute the command at the specified time. You don't need to install anything. The only two lines of code you will need to modify are:

    fileSpec = "C:\Scripts\RunTask.bat"
    scheduledTime = Int(Now) + TimeSerial(15, 00, 00)

    Where the value of fileSpec is the UNC path of your batch file (in double quotes if there are spaces in the file path!) and the value contained within the TimeSerial Statement is the time you want to task to execute. EG TimeSerial(23, 30, 00) if you wanted the batch file to be executed at 11:30PM.

    As a simple test can you try changing the content of your batch file to "ping local" host and the location of the batch file to "C:\Scripts\RunTask.bat". Run the script again and verify the batch file execution works on your XP system. Please post further information:

    • The content of the batch file you want to execute.
    • The UNC path of the batch file to be executed.
    • The effective permissions of the user executing the batch file?
    • system specific settings IE domain or workgroup, any software\group policy restrictions.

    Consider the production documenation and limitations:

    http://www.truecrypt.org/docs/?s=how-to-back-up-securely

    http://www.truecrypt.org/docs/?s=non-admin-users

    Have you tested the batch file runs sucessfully when executed manually as the domain user on XP? Also please note that if you want this to run on a daily basis then remove the "Exit Function" statement as this causes the script to terminate once the batch file is run and if your user's stay logged on for days then it would only run once. Furthermore if your environment does implement AD\group policy and the users have been restricted from accessing the command prompt (note the batch file is executed using "cmd /c") or prevented from running batch files via software restriction policies then naturally it won't work.

    Let me know how it goes

    Cheers Matt :)

     

    • Marked as answer by M. AMIR Monday, July 19, 2010 4:17 PM
    Sunday, July 18, 2010 5:33 AM
    Moderator
  • Hi

    Yes there is, here is a technique i use alot. Have the script write a dword value and the current date in reg_sz value. For example: 

    HKCU\System\%netbiosDomainName%\Scripts\%ScriptName%\BackupComplete = 1
    HKCU\System\%netbiosDomainName%\Scripts\%ScriptName%\ScriptDateTime = "20-07-2010"

    When the user logs on you have your logon script read those registry keys (because the values are stored in HKCU the user's have permissions to read\write to it...really doesn't matter whereabout in HKCU, you could store it it "HKCU\Scripts\Logon" if you want, just change the value of the regPath variable), compare the current date and the dword value. If the date is not the current date or the dword value is 0 then the script needs to run again. If the dword value is 0 and the it's the current date then the backup failed so run it again. If it date is not the current date reset the dword value to 0 and run the script again. Try this...haven't fully it tested it yet but it should give you some ideas

    Cheers Matt :)

    '---------------------------------------------------------------------------------------------------------------------------- 
    'Script Name : RunTask.vbs  
    'Author   : Matthew Beattie  
    'Created   : 17/07/10
    'Description : This script runs as a backgroud process in an infinate loop. It wakes up every 10 minutes to determine if 
    '      : the current time is 3PM on a weekday. If it is it attempts to execute a local batch file otherwise it will 
    '      : wait 590 seconds (almost 10 minutes) to allow the system 10 seconds to wake up if the system is busy 
    '      : processing.
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Initialization Section  
    '---------------------------------------------------------------------------------------------------------------------------- 
    Option Explicit
    Dim objFSO, wshShell, wshNetwork, netBiosDomain, scriptBaseName
    On Error Resume Next 
      Set objFSO   = CreateObject("Scripting.FileSystemObject")
      Set wshNetwork = CreateObject("WScript.Network")
      Set wshShell  = CreateObject("Wscript.Shell")
      scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
      netBiosDomain = wshNetwork.UserDomain
      ProcessScript
      If Err.Number <> 0 Then  
       Wscript.Quit  
      End If  
    On Error Goto 0  
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Functions Processing Section  
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ProcessScript -> Primary Function that controls all other script processing.
    'Parameters : None     -> 
    'Return   : None     -> 
    '----------------------------------------------------------------------------------------------------------------------------
    Function ProcessScript
      Dim fileSpec, required, completed
      Dim scriptDate, currentDate, scriptTime, regPath
      '-------------------------------------------------------------------------------------------------------------------------
      'Assign local variables.
      '-------------------------------------------------------------------------------------------------------------------------
      fileSpec  = "C:\Scripts\VBScript\Projects\RunTask\RunTask.bat"
      regPath   = "HKCU\System\" & netBiosDomain & "\" & scriptBaseName
      completed  = ReadRegistry(regPath & "\Backup")
      scriptDate = ReadRegistry(regPath & "\ScriptDate")
      scriptTime = "22:58:00"
      currentDate = IsoDateString(Now)
      required  = False
      '-------------------------------------------------------------------------------------------------------------------------
      'Ensure the script is processed if there was an error reading scripts registry values or they are blank.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       If scriptDate = "" Or completed = "" Then
         required = True
         Exit Do
       Else
         '-------------------------------------------------------------------------------------------------------------------
         'Ensure the script is processed if script's registry date value is not a valid date.
         '-------------------------------------------------------------------------------------------------------------------
         If Not IsDate(scriptDate) Then
          scriptDate = DateAdd("d", -1, currentDate)
          required  = True
          Exit Do
         End If
       End If
       '----------------------------------------------------------------------------------------------------------------------
       'Ensure the script exits if it's already beeen successfully processed today.
       '----------------------------------------------------------------------------------------------------------------------
       If DateDiff("d", scriptDate, currentDate) = 0 And completed = 1 Then
         Exit Do
       Else
         required = True
       End If
      Loop Until True
      '-------------------------------------------------------------------------------------------------------------------------
      'Reset the script's backup registry flag to if required and run the scheduled process otherwise exit.
      '-------------------------------------------------------------------------------------------------------------------------
      If required Then
       On Error Resume Next
         wshShell.RegWrite regPath & "\Backup", 0, "REG_DWORD"
         If Err.Number <> 0 Then
          Exit Function
         End If
       On Error Goto 0
    
       '----------------------------------------------------------------------------------------------------------------------
       'Run the script or batch file and write the results to users registry profile.
       '----------------------------------------------------------------------------------------------------------------------
       On Error Resume Next
         If Not ExecuteScript(fileSpec, scriptTime) Then
          wshShell.RegWrite regPath & "\Backup", 0, "REG_DWORD"
          wshShell.RegWrite regPath & "\ScriptDate", currentDate, "REG_SZ"
          If Err.Number <> 0 Then
            Exit Function
          End If
         Else
          wshShell.RegWrite regPath & "\Backup", 1, "REG_DWORD"
          wshShell.RegWrite regPath & "\ScriptDate", currentDate, "REG_SZ"
          If Err.Number <> 0 Then
            Exit Function
          End If
         End If
       On Error Goto 0
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ExecuteScript -> Place double quotes around a string and replace double quotes
    'Parameters : fileSpec   -> String containing the UNC path of the script to execute in without quotes.
    '      : scriptTime  -> String containing the ISO Time Value. EG 23:30:00 (11:30PM)
    'Return   : ExecuteScript -> Double quoted string.
    '----------------------------------------------------------------------------------------------------------------------------
    Function ExecuteScript(fileSpec, scriptTime)
      Dim scheduledTime, waited, scriptHours, scriptMinutes, scriptSeconds
      ExecuteScript = False
      waited    = 0
      '-------------------------------------------------------------------------------------------------------------------------
      'Ensure the Script exists and the execute time is a valid time.
      '-------------------------------------------------------------------------------------------------------------------------
      If Not objFSO.FileExists(fileSpec) Then
       Exit Function
      End If
      If VarType(scriptTime) <> vbString Then
       Exit Function
      End If
      If InStr(1, scriptTime, ":", vbTextCompare) = 0 Then
       Exit Function
      End If
      On Error Resume Next
       scriptHours  = Split(scriptTime, ":")(0)
       scriptMinutes = Split(scriptTime, ":")(1)
       scriptSeconds = Split(scriptTime, ":")(2)
       If Err.Number <> 0 Then
         Exit Function
       End If
      On Error Goto 0
      '-------------------------------------------------------------------------------------------------------------------------
      'Run the script as a background process in an infinate loop. Use task manager to terminate "wscript.exe" to cancel it.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       '----------------------------------------------------------------------------------------------------------------------
       'Determine the exact time to run the process the Calculate the number of seconds waited.
       '----------------------------------------------------------------------------------------------------------------------
       scheduledTime = Int(Now) + TimeSerial(scriptHours, scriptMinutes, scriptSeconds)
       waited    = DateDiff("S", Now, scheduledTime)
       '----------------------------------------------------------------------------------------------------------------------
       'Run the process if it's time to execute the batch file otherwise continue waiting
       '----------------------------------------------------------------------------------------------------------------------
       If waited <= 600 Then
         Wscript.Sleep waited * 1000
         '----------------------------------------------------------------------------------------------------------------
         'Ensure the Batch file exists. Terminate the script from running in an infinate loop if it doesn't exist.
         '----------------------------------------------------------------------------------------------------------------
         If objFSO.FileExists(fileSpec) Then
          On Error Resume Next
            wshShell.Run "cmd /c " & DQ(fileSpec), ,False
            If Err.Number <> 0 Then
             Exit Function
            Else
             ExecuteScript = True
             Exit Function
            End If
          On Error Goto 0
         End If
       Else
         WScript.Sleep 590 * 1000
       End If
      Loop
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : DQ     -> Place double quotes around a string and replace double quotes
    '      :       -> within the string with pairs of double quotes.
    'Parameters : stringValue -> String value to be double quoted
    'Return   : DQ     -> Double quoted string.
    '----------------------------------------------------------------------------------------------------------------------------
    Function DQ (ByVal stringValue)
      If stringValue <> "" Then
       DQ = """" & Replace (stringValue, """", """""") & """"
      Else
       DQ = """"""
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ReadRegistry -> Read the value of a registry key or value.
    'Parameters : key     -> Name of the key (ending in "\") or value to read.
    'Return   : ReadRegistry -> Value of key or value read from the local registry (blank is not found).
    '----------------------------------------------------------------------------------------------------------------------------
    Function ReadRegistry(ByVal key)
      Dim result
      If StrComp(Left (key, 4), "HKU\", vbTextCompare) = 0 Then
       Key = "HKEY_USERS" & Mid (key, 4)
      End If
      On Error Resume Next
       ReadRegistry = WshShell.RegRead (key)
       If Err.Number <> 0 Then
         ReadRegistry = ""
       End If
      On Error Goto 0
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : IsoDateString -> Generate an ISO date string from a date/time value.
    'Parameters : dateValue   -> Input date/time value.
    'Return   : IsoDateString -> Date part of the input value in "yyyy-mm-dd" format.
    '----------------------------------------------------------------------------------------------------------------------------
    Function IsoDateString(dateValue)
      If IsDate(dateValue) Then
       IsoDateString = Right ("000" & Year (dateValue), 4) & "-" & _
               Right ( "0" & Month (dateValue), 2) & "-" & _
               Right ( "0" &  Day (dateValue), 2)
      Else
       IsoDateString = "0000-00-00"
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    
    • Marked as answer by M. AMIR Sunday, July 25, 2010 1:06 PM
    Tuesday, July 20, 2010 1:01 PM
    Moderator

All replies

  • Hi

    Here is something similar i've previously posted.

    http://social.technet.microsoft.com/Forums/en/ITCG/thread/2e9b1f0e-3086-405d-a1d7-dad354e2125c

    This can be easily modified so that it will execute a command\batch file using the run method of the wshShell object at a specified date\time. Please provide some information on what your trying to achieve with the batch file and when you want to run it and i'll update the code for you.

    Cheers Matt :)

    Saturday, July 17, 2010 11:52 AM
    Moderator
  • Hi

    I'd imagine something like this should do the trick...just change the variable "filespec" to the location of the batch file you want to run and set the time in the TimeSerial function. For example "TimeSerial(15, 00, 00)" = 3PM.

    Do you want to run this batch file every day or once only for all users? This will exit after the batch file has run at the specified time. Here is the code. Hope that helps

    Cheers Matt :)

    '---------------------------------------------------------------------------------------------------------------------------- 
    'Script Name : RunTask.vbs   
    'Author   : Matthew Beattie   
    'Created   : 17/07/10
    'Description : This script runs as a backgroud process in an infinate loop. It wakes up every 10 minutes to determine if 
    '      : the current time is 3PM on a weekday. If it is it attempts to execute a local batch file otherwise it will 
    '      : wait 590 seconds (almost 10 minutes) to allow the system 10 seconds to wake up if the system is busy 
    '      : processing.
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Initialization Section   
    '---------------------------------------------------------------------------------------------------------------------------- 
    Option Explicit
    Dim objFSO, wshShell
    On Error Resume Next  
      Set objFSO  = CreateObject("Scripting.FileSystemObject")
      Set wshShell = CreateObject("Wscript.Shell")
      ProcessScript
      If Err.Number <> 0 Then   
       Wscript.Quit   
      End If   
    On Error Goto 0   
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Functions Processing Section   
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ProcessScript -> Primary Function that controls all other script processing.
    'Parameters : None     -> 
    'Return   : None     -> 
    '----------------------------------------------------------------------------------------------------------------------------
    Function ProcessScript
      Dim scheduledTime, fileSpec, waited, i
      fileSpec = "C:\Scripts\RunTask.bat"
      '-------------------------------------------------------------------------------------------------------------------------
      'Run the script as a background process in an infinate loop. Use task manager to terminate "wscript.exe" to cancel it.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       '----------------------------------------------------------------------------------------------------------------------
       'Determine the exact time to run the process the Calculate the number of seconds waited.
       '----------------------------------------------------------------------------------------------------------------------
       scheduledTime = Int(Now) + TimeSerial(15, 00, 00)
       waited    = DateDiff("S", Now, scheduledTime)
       '----------------------------------------------------------------------------------------------------------------------
       'Run the process if it's time to execute the batch file otherwise continue waiting
       '----------------------------------------------------------------------------------------------------------------------
       On Error Resume Next
         If waited <= 600 Then
          Wscript.Sleep waited * 1000
          '----------------------------------------------------------------------------------------------------------------
          'Ensure the Batch file exists. Terminate the script from running in an infinate loop if it doesn't exist.
          '----------------------------------------------------------------------------------------------------------------
          If objFSO.FileExists(fileSpec) Then
            wshShell.Run "cmd /c " & fileSpec, ,False
            If Err.Number <> 0 Then
             WScript.Quit
            End If
            Exit Function
          End If
         Else
          WScript.Sleep 590 * 1000
         End If
       On Error Goto 0
      Loop
    End Function
    Saturday, July 17, 2010 12:20 PM
    Moderator
  • The standard method in Windows is to create a scheduled task that invokes your script at the specified time.
    Saturday, July 17, 2010 12:41 PM
  • Hi

    Yeah, I agree a schedule task is always the prefered method whenever possible, EG:

    schtasks.exe /create /s \\hostname /ru system /sc daily /tn RunCommand /tr C:\Scripts\RunCommand.vbs /st 15:00:00

    Sure no problem for an administrator to create the scheduled task as local system...however if you need it to run explicitly as the user logging on (i'd assumed this is the case and hopefully a standard user without admin rights) then creating a scheduled task becomes an issue as credentials are required to create the scheduled task.

    Having the user execute a script at logon which waits as a background process, wakes up at regular intervals and waits until a specified date\time isn't ideal as the user has rights to terminate the WScript process they initiated but it does avoids the need to provide credentials and ensures the batch file is executed within the users security context. I'm open to other suggestions if you know of any other ways to achieve this?

    Cheers Matt :)

     

    Saturday, July 17, 2010 1:10 PM
    Moderator
  • Hi Matthew.

    Thanks for your attention. As i told before, I'm trying to run a batch file for all of the domain users in a specifyed time. The batch file is about taking backup from their files and putting them on a secure place that nobody exept themselves can't see the secure place. So i can't use windows scheduler. I thing the best way to handle this scenario is using a vbs script in the "Active Directory logon Script" for all users and set a time for executing the batch file for every day in that script. It must not be hard for writing this script, but unfortunately i'm not familiar enough with scripting.

    • Edited by M. AMIR Saturday, July 17, 2010 6:25 PM
    Saturday, July 17, 2010 6:18 PM
  • Domain admins or backup operators usually have access to all folders. If you get the scheduled task to run under the domain admin account then it won't have a problem copying the file. It's the easiest and cleanest solution. Remember to protect the batch file or script so that users cannot turn it into a trojan horse . . .

    If neither of these has access to the folder in question, how do you back it up?

    Saturday, July 17, 2010 6:23 PM
  • As i said before, in this scenario users have a secured directory which nobody can see that exept themselves. The secured directory is made by a third party open source security program called Truecrypt, that's why i can't see that directory. The program gives me an encrypted file which the file is exactly like a drive for users. Your suggestion is a very good solution and i was trying to do it at first. By the way, thanks for the warning about protecting the batch file.
    Saturday, July 17, 2010 7:16 PM
  • For future posts it would help if you described your environment in detail. Respondents in this forum are quite familiar with TrueCrypt and would tailor their responses accordingly - unless you conceal this key detail.

    Saturday, July 17, 2010 8:36 PM
  • Hi Matthew

    I have tested this script and it works on windows 7. But it dose not work on XP! Is there any program that i have to install to running this script or what?

    Thanks

    Sunday, July 18, 2010 4:31 AM
  • Hi

    I tested it on XP SP3 (WScript 5.7) where the content of the batch file just executes  a simple "ping localhost". It Works fine for me and opens a command prompt to execute the command at the specified time. You don't need to install anything. The only two lines of code you will need to modify are:

    fileSpec = "C:\Scripts\RunTask.bat"
    scheduledTime = Int(Now) + TimeSerial(15, 00, 00)

    Where the value of fileSpec is the UNC path of your batch file (in double quotes if there are spaces in the file path!) and the value contained within the TimeSerial Statement is the time you want to task to execute. EG TimeSerial(23, 30, 00) if you wanted the batch file to be executed at 11:30PM.

    As a simple test can you try changing the content of your batch file to "ping local" host and the location of the batch file to "C:\Scripts\RunTask.bat". Run the script again and verify the batch file execution works on your XP system. Please post further information:

    • The content of the batch file you want to execute.
    • The UNC path of the batch file to be executed.
    • The effective permissions of the user executing the batch file?
    • system specific settings IE domain or workgroup, any software\group policy restrictions.

    Consider the production documenation and limitations:

    http://www.truecrypt.org/docs/?s=how-to-back-up-securely

    http://www.truecrypt.org/docs/?s=non-admin-users

    Have you tested the batch file runs sucessfully when executed manually as the domain user on XP? Also please note that if you want this to run on a daily basis then remove the "Exit Function" statement as this causes the script to terminate once the batch file is run and if your user's stay logged on for days then it would only run once. Furthermore if your environment does implement AD\group policy and the users have been restricted from accessing the command prompt (note the batch file is executed using "cmd /c") or prevented from running batch files via software restriction policies then naturally it won't work.

    Let me know how it goes

    Cheers Matt :)

     

    • Marked as answer by M. AMIR Monday, July 19, 2010 4:17 PM
    Sunday, July 18, 2010 5:33 AM
    Moderator
  • Hi Matthew

    Thanks for your great descriptions. It's working now. The Problem was in your second item "The UNC path of the batch file to be executed. "

    It seems there is some limitations for executing such scripts in C: drive. I have moved it to the D: drive and it works fine.

     You are such nice guy. Thank you thank you a lot Matthew

    Monday, July 19, 2010 4:14 PM
  • You know what. There is a little problem with that. As i said before, i want to run it by the "Logon Script" for all domain. Now, every time i reboot a client, the script will work and it goes for backing up again. Is there any way to tel it that you must not run again if you did this befor in today. In fact i need it to run once per day.

    Thanks 

    Tuesday, July 20, 2010 7:56 AM
  • You need to maintain a semaphore file with the following logic:
    - If the semaphore file does not exist: Create the semaphore file, then run the script.
    - If the semaphore file's date is the same as today's date: Don't do anything.
    - If the semaphore file's date is not the same as today's date: Update the semaphore file, then run the script.
    Tuesday, July 20, 2010 10:32 AM
  • Hi

    Yes there is, here is a technique i use alot. Have the script write a dword value and the current date in reg_sz value. For example: 

    HKCU\System\%netbiosDomainName%\Scripts\%ScriptName%\BackupComplete = 1
    HKCU\System\%netbiosDomainName%\Scripts\%ScriptName%\ScriptDateTime = "20-07-2010"

    When the user logs on you have your logon script read those registry keys (because the values are stored in HKCU the user's have permissions to read\write to it...really doesn't matter whereabout in HKCU, you could store it it "HKCU\Scripts\Logon" if you want, just change the value of the regPath variable), compare the current date and the dword value. If the date is not the current date or the dword value is 0 then the script needs to run again. If the dword value is 0 and the it's the current date then the backup failed so run it again. If it date is not the current date reset the dword value to 0 and run the script again. Try this...haven't fully it tested it yet but it should give you some ideas

    Cheers Matt :)

    '---------------------------------------------------------------------------------------------------------------------------- 
    'Script Name : RunTask.vbs  
    'Author   : Matthew Beattie  
    'Created   : 17/07/10
    'Description : This script runs as a backgroud process in an infinate loop. It wakes up every 10 minutes to determine if 
    '      : the current time is 3PM on a weekday. If it is it attempts to execute a local batch file otherwise it will 
    '      : wait 590 seconds (almost 10 minutes) to allow the system 10 seconds to wake up if the system is busy 
    '      : processing.
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Initialization Section  
    '---------------------------------------------------------------------------------------------------------------------------- 
    Option Explicit
    Dim objFSO, wshShell, wshNetwork, netBiosDomain, scriptBaseName
    On Error Resume Next 
      Set objFSO   = CreateObject("Scripting.FileSystemObject")
      Set wshNetwork = CreateObject("WScript.Network")
      Set wshShell  = CreateObject("Wscript.Shell")
      scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
      netBiosDomain = wshNetwork.UserDomain
      ProcessScript
      If Err.Number <> 0 Then  
       Wscript.Quit  
      End If  
    On Error Goto 0  
    '---------------------------------------------------------------------------------------------------------------------------- 
    'Functions Processing Section  
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ProcessScript -> Primary Function that controls all other script processing.
    'Parameters : None     -> 
    'Return   : None     -> 
    '----------------------------------------------------------------------------------------------------------------------------
    Function ProcessScript
      Dim fileSpec, required, completed
      Dim scriptDate, currentDate, scriptTime, regPath
      '-------------------------------------------------------------------------------------------------------------------------
      'Assign local variables.
      '-------------------------------------------------------------------------------------------------------------------------
      fileSpec  = "C:\Scripts\VBScript\Projects\RunTask\RunTask.bat"
      regPath   = "HKCU\System\" & netBiosDomain & "\" & scriptBaseName
      completed  = ReadRegistry(regPath & "\Backup")
      scriptDate = ReadRegistry(regPath & "\ScriptDate")
      scriptTime = "22:58:00"
      currentDate = IsoDateString(Now)
      required  = False
      '-------------------------------------------------------------------------------------------------------------------------
      'Ensure the script is processed if there was an error reading scripts registry values or they are blank.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       If scriptDate = "" Or completed = "" Then
         required = True
         Exit Do
       Else
         '-------------------------------------------------------------------------------------------------------------------
         'Ensure the script is processed if script's registry date value is not a valid date.
         '-------------------------------------------------------------------------------------------------------------------
         If Not IsDate(scriptDate) Then
          scriptDate = DateAdd("d", -1, currentDate)
          required  = True
          Exit Do
         End If
       End If
       '----------------------------------------------------------------------------------------------------------------------
       'Ensure the script exits if it's already beeen successfully processed today.
       '----------------------------------------------------------------------------------------------------------------------
       If DateDiff("d", scriptDate, currentDate) = 0 And completed = 1 Then
         Exit Do
       Else
         required = True
       End If
      Loop Until True
      '-------------------------------------------------------------------------------------------------------------------------
      'Reset the script's backup registry flag to if required and run the scheduled process otherwise exit.
      '-------------------------------------------------------------------------------------------------------------------------
      If required Then
       On Error Resume Next
         wshShell.RegWrite regPath & "\Backup", 0, "REG_DWORD"
         If Err.Number <> 0 Then
          Exit Function
         End If
       On Error Goto 0
    
       '----------------------------------------------------------------------------------------------------------------------
       'Run the script or batch file and write the results to users registry profile.
       '----------------------------------------------------------------------------------------------------------------------
       On Error Resume Next
         If Not ExecuteScript(fileSpec, scriptTime) Then
          wshShell.RegWrite regPath & "\Backup", 0, "REG_DWORD"
          wshShell.RegWrite regPath & "\ScriptDate", currentDate, "REG_SZ"
          If Err.Number <> 0 Then
            Exit Function
          End If
         Else
          wshShell.RegWrite regPath & "\Backup", 1, "REG_DWORD"
          wshShell.RegWrite regPath & "\ScriptDate", currentDate, "REG_SZ"
          If Err.Number <> 0 Then
            Exit Function
          End If
         End If
       On Error Goto 0
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ExecuteScript -> Place double quotes around a string and replace double quotes
    'Parameters : fileSpec   -> String containing the UNC path of the script to execute in without quotes.
    '      : scriptTime  -> String containing the ISO Time Value. EG 23:30:00 (11:30PM)
    'Return   : ExecuteScript -> Double quoted string.
    '----------------------------------------------------------------------------------------------------------------------------
    Function ExecuteScript(fileSpec, scriptTime)
      Dim scheduledTime, waited, scriptHours, scriptMinutes, scriptSeconds
      ExecuteScript = False
      waited    = 0
      '-------------------------------------------------------------------------------------------------------------------------
      'Ensure the Script exists and the execute time is a valid time.
      '-------------------------------------------------------------------------------------------------------------------------
      If Not objFSO.FileExists(fileSpec) Then
       Exit Function
      End If
      If VarType(scriptTime) <> vbString Then
       Exit Function
      End If
      If InStr(1, scriptTime, ":", vbTextCompare) = 0 Then
       Exit Function
      End If
      On Error Resume Next
       scriptHours  = Split(scriptTime, ":")(0)
       scriptMinutes = Split(scriptTime, ":")(1)
       scriptSeconds = Split(scriptTime, ":")(2)
       If Err.Number <> 0 Then
         Exit Function
       End If
      On Error Goto 0
      '-------------------------------------------------------------------------------------------------------------------------
      'Run the script as a background process in an infinate loop. Use task manager to terminate "wscript.exe" to cancel it.
      '-------------------------------------------------------------------------------------------------------------------------
      Do
       '----------------------------------------------------------------------------------------------------------------------
       'Determine the exact time to run the process the Calculate the number of seconds waited.
       '----------------------------------------------------------------------------------------------------------------------
       scheduledTime = Int(Now) + TimeSerial(scriptHours, scriptMinutes, scriptSeconds)
       waited    = DateDiff("S", Now, scheduledTime)
       '----------------------------------------------------------------------------------------------------------------------
       'Run the process if it's time to execute the batch file otherwise continue waiting
       '----------------------------------------------------------------------------------------------------------------------
       If waited <= 600 Then
         Wscript.Sleep waited * 1000
         '----------------------------------------------------------------------------------------------------------------
         'Ensure the Batch file exists. Terminate the script from running in an infinate loop if it doesn't exist.
         '----------------------------------------------------------------------------------------------------------------
         If objFSO.FileExists(fileSpec) Then
          On Error Resume Next
            wshShell.Run "cmd /c " & DQ(fileSpec), ,False
            If Err.Number <> 0 Then
             Exit Function
            Else
             ExecuteScript = True
             Exit Function
            End If
          On Error Goto 0
         End If
       Else
         WScript.Sleep 590 * 1000
       End If
      Loop
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : DQ     -> Place double quotes around a string and replace double quotes
    '      :       -> within the string with pairs of double quotes.
    'Parameters : stringValue -> String value to be double quoted
    'Return   : DQ     -> Double quoted string.
    '----------------------------------------------------------------------------------------------------------------------------
    Function DQ (ByVal stringValue)
      If stringValue <> "" Then
       DQ = """" & Replace (stringValue, """", """""") & """"
      Else
       DQ = """"""
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : ReadRegistry -> Read the value of a registry key or value.
    'Parameters : key     -> Name of the key (ending in "\") or value to read.
    'Return   : ReadRegistry -> Value of key or value read from the local registry (blank is not found).
    '----------------------------------------------------------------------------------------------------------------------------
    Function ReadRegistry(ByVal key)
      Dim result
      If StrComp(Left (key, 4), "HKU\", vbTextCompare) = 0 Then
       Key = "HKEY_USERS" & Mid (key, 4)
      End If
      On Error Resume Next
       ReadRegistry = WshShell.RegRead (key)
       If Err.Number <> 0 Then
         ReadRegistry = ""
       End If
      On Error Goto 0
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    'Name    : IsoDateString -> Generate an ISO date string from a date/time value.
    'Parameters : dateValue   -> Input date/time value.
    'Return   : IsoDateString -> Date part of the input value in "yyyy-mm-dd" format.
    '----------------------------------------------------------------------------------------------------------------------------
    Function IsoDateString(dateValue)
      If IsDate(dateValue) Then
       IsoDateString = Right ("000" & Year (dateValue), 4) & "-" & _
               Right ( "0" & Month (dateValue), 2) & "-" & _
               Right ( "0" &  Day (dateValue), 2)
      Else
       IsoDateString = "0000-00-00"
      End If
    End Function
    '----------------------------------------------------------------------------------------------------------------------------
    
    • Marked as answer by M. AMIR Sunday, July 25, 2010 1:06 PM
    Tuesday, July 20, 2010 1:01 PM
    Moderator
  • Thank you thousand times. You are the man. I will tell you if there was any problem, however it seems great.
    Sunday, July 25, 2010 1:09 PM