Trying get .vbs to run in scheduler when logged off

Posted on 2011-04-21
Last Modified: 2012-05-11
I have a working vb script that performs a backup of 2 servers.  Also in the script I am mapping drives and copying the .bkf files over to the main server that gets backed up to tape.  Whenever Im logged in and run the script everything works fine.  Also if Im logged in and set the script to run in the task scheduler everything runs fine.  My goal is to have the script run when logged off using the task scheduler.  I have done extensive research on this and cant seem to figure it out.  The scheduler reports have no record/log of the task running and nothing within the event viewer.  Any help would be great!
Option Explicit 
Const NTBUPathName = "C:\WINDOWS\System32\ntbackup.exe"  'Pathname to NTBackUp.exe 
Const BKFDirVCS = "4mm DDS"   'Folder for back-up files
Const BKFDirCA = "D:\Backup\"
Const BKFDirCAtoVCS = "Z:\Backup\" 
Const BKSDir = "C:\Backup Scripts\"   'Folder for selection file  
Const BKSFileVCS = "VCSbackup.bks"  'Selection file name
Const BKSFileCA = "CAbackup.bks"  'Selection file name
Const BKFPrefixVCS = "VCSbackup_"   'Prefix for back-up file
Const BKFPrefixCA = "CAbackup_"   'Prefix for back-up file
Const BKFExtension = "bkf"       'File extension for back-up files (bkf) 
Const KeepDays = 2              'Number of days to keep back-up files 
Dim oFileSys
Dim oFolder 
Dim oFileList 
Dim oFile 
Dim dPurgeDate 
Dim iBKFPrefixLength  
Dim sISODate, sCommandVCS, sCommandCA, intReturn 
Dim oWSH, objFSO, objNet, strCompName, objShell
Dim Network, WshNet, strDrive
' Part 1 - Back up files according to selection file (BKS extension) 
' into a new archive file (BKF extension) with datestamp in file name 
sISODate = FormatNumber(Year(Date),0,,,vbFalse) & _ 
  Right("0" & FormatNumber(Month(Date),0), 2) & _ 
  Right("0" & FormatNumber(Day(Date),0), 2) 
sCommandVCS = NTBUPathName & " backup ""@" & BKSDir & BKSFileVCS & """ " & "/a /t ""Media Created 4-19-2011"" " & "/d " & """Set created " & Now & """ /v:yes /r:yes /rs:no /hc:on /m normal /j ""VCSbackup"" /l:f"
sCommandCA = NTBUPathName & " backup ""@" & BKSDir & BKSFileCA & """ " & "/d ""Set created " & Now & """ /v:yes /r:no /rs:no /hc:off /m normal /j ""CAbackup"" /l:s " & "/f """ & BKFDirCA & BKFPrefixCA & sISODate & ".bkf"""  
' If you want to see the full command line (for testing), uncomment the next line 
'WScript.Echo sCommandVCS
'Wscript.Echo sCommandCA 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNet = CreateObject("WScript.Network")
strCompName = objNet.ComputerName

	'Backup only if run from CA
	IF left(strCompName,8) = "SERVER01" THEN
		Set objShell = CreateObject("WScript.Shell")
			intReturn = objShell.Run(sCommandCA, 1, True)

		'Map network drive from CA to VCS
 		Set Network = CreateObject("") 
		Network.MapNetworkDrive "Z:", "\\server02\d$"

		'Copy files from CA to VCS
		Dim sOriginFolder, sDestinationFolder, sFile, oFSO 
 			Set oFSO = CreateObject("Scripting.FileSystemObject") 
 			sOriginFolder = "D:\Backup" 
 			sDestinationFolder = "Z:\Backup" 
 				For Each sFile In oFSO.GetFolder(sOriginFolder).Files 
  				If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then 
   				oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True 
  				End If 

' Part 2 - Delete old CA backups (BKF Files) by 
' looping thru all files meeting this filespec and 
' for each one, delete it if its DateCreated is older than KeepDays days. 
dPurgeDate = Date - KeepDays 
iBKFPrefixLength = Len(BKFPrefixCA) 
Set oFileSys = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFileSys.GetFolder(BKFDirCAtoVCS) 
Set oFileList = oFolder.Files 
For Each oFile in oFileList 
  If (oFile.DateCreated <= dPurgeDate) And _ 
    (Left(oFile.Name, iBKFPrefixLength) = BKFPrefixCA) And _ 
    (Right(oFile.Name, 3) = BKFExtension) Then 
    oFile.Delete True 
  End If 

			'Disconnect mapped drive to VCS
			Set WshNet = WScript.CreateObject("WScript.Network")
			strDrive = "Z:"
			WshNet.RemoveNetworkDrive strDrive, true, True

			Const DeleteReadOnly = TRUE

			'Delete .bkf file just created on CA
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			objFSO.DeleteFile("D:\Backup\*.bkf"), DeleteReadOnly
	End If

	'Backup only if run from VCS
	IF left(strCompName,8) = "SERVER02" THEN
		Set objShell = CreateObject("WScript.Shell")
			intReturn = objShell.Run(sCommandVCS, 1, True)
	End If

Open in new window

Question by:PKI_Program_Office
    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    the is a group policy allow run as batch job...

    adding the user (under which the scheduled task is to run) to "log on as batch job" in group policy

    Local Computer Policy
    Computer Configuration
    Windows Settings
    Security Settings
    Local Polices
    User Rights Assignment

    Author Comment

    Yes.  I had this already configured "log on as batch job" to be assigned to the same admin user that I have assigned in the scheduler.
    LVL 44

    Expert Comment

    * Does the scheduled task have the correct userID and password?
    * Have you set the scheduled task to wake up?

    Expert Comment

    yes. The correct userid and passwd was applied.  I was not aware of any wakeup options in the task scheduler. Can you elaborate a little more on that?
    LVL 44

    Expert Comment

    open up the property/setup dialog
    Depending on your OS, you might have to look at different tabs until you see the Wake up checkbox.  On Vista, it is on the Conditions tab.
    LVL 32

    Expert Comment

    by:Robberbaron (robr)
    The only issue I have had on WS08 is with the "Run as batch" permission.

    Try a very basic batch file  & see what happens.  This is an attempt to isolate the issue away from a logon to network issue

    dir c: > c:\test.log

    set it up as a scheduled item with the desired user.
    LVL 44

    Expert Comment

    Is D: a mapped drive?
    LVL 25

    Accepted Solution

    What OS?

    Is the file running, but not showing up in event log?

    I have a job in Win7, that runs, even if no one is logged in, but doesn't show up in the task scheduler, etc.  However my job copies data from a server to a local directory using a batch file. It does work, but it runs as the SYSTEM account.

    In win7 make sure that "Run whether user is logged on or not" is checked. You may also need to check "Run with highest privileges"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
    Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now