Link to home
Start Free TrialLog in
Avatar of PKI_Program_Office
PKI_Program_Office

asked on

Trying get .vbs to run in scheduler when logged off

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("Wscript.network") 
		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 
 				Next 

' 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 
Next 

			'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

Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

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
Avatar of PKI_Program_Office
PKI_Program_Office

ASKER

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.
* Does the scheduled task have the correct userID and password?
* Have you set the scheduled task to wake up?
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?
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.
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

eg...
dir c: > c:\test.log

set it up as a scheduled item with the desired user.
Is D: a mapped drive?
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial