[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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

0
PKI_Program_Office
Asked:
PKI_Program_Office
1 Solution
 
Robberbaron (robr)Commented:
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
0
 
PKI_Program_OfficeAuthor Commented:
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.
0
 
aikimarkCommented:
* Does the scheduled task have the correct userID and password?
* Have you set the scheduled task to wake up?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cmcnultyCommented:
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?
0
 
aikimarkCommented:
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.
0
 
Robberbaron (robr)Commented:
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.
0
 
aikimarkCommented:
Is D: a mapped drive?
0
 
SStoryCommented:
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"
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now