Solved

Run a .vbs Script as a Scheduled task on Windows Server 2008 R2

Posted on 2012-03-18
15
8,915 Views
Last Modified: 2012-03-27
Hello

I am trying to run a /vbs script as a Scheduled task on a Windows Server.
For Testing I have just got the following in the script.


 on error resume next
 
 msgbox "Before Starting Excel"

Set xlApp = Nothing
Set xlApp = GetObject(,"Excel.Application")
if xlApp is Nothing then
      Set xlApp = CreateObject("Excel.Application")
end if
 msgbox "Excel should be running - (Check in the task manager Processes tab)"
xlApp.visible = False

xlApp.visible = true


If I run the script from a short cut on my local machine it runs fine.
If I manualy run it as a scheduled task from my local machine it runs fine.
From my local machine it runs as a scheduled task fine.

If I login to the Server and manualy run the script from a short cut it runs fine.
If I try to run it manualy from the task scheduler on the server nothing happens.
From the Server nothing happens when it is set to run as a scheduled task.

Any Suggestions?
0
Comment
Question by:p-plater
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:Navdeep
ID: 37736108
Hi,

create a batch file and mention the following lines
c:\windows\System32\cmd.exe /c cscript "D:\temp\test.vbs"

save the file as fileName.bat and now in schedule task specify the batch file instead of vbs file.

Try and see if that helps you.

Regards,
Navdeep [v-2nas]
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37736230
You won't need the cmd /c.  You can just use
cscript "D:\temp\test.vbs"

Make sure that for testing you schedule the task to run as the same user account as you are logged in with.

See if that helps.  Also, make the test script output some text to a file, like
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Test.txt", True)
objFile.WriteLine Now
objFile.Close

and then see if the file exists.  It may just be that the script is not running interactively.

Regards,

Rob.
0
 

Author Comment

by:p-plater
ID: 37736400
OK
The text file is created successfuly.
How do I code it to
1. Open Excel if it is not running
2. Run a Macro in a specific workbook
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37736415
Something like this should work

strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Test.xls"
Set objExcel = CreateObject("Excel.Application")
Set objWB1 = objExcel.Workbooks.Open(strExcelFile, False, False)
objExcel.Visible = True
objWB1.Application.Run "Macro1"
objWB1.Close True
objExcel.Quit

Open in new window


Regards,

Rob.
0
 
LVL 12

Expert Comment

by:S00007359
ID: 37736685
in server 2008;

there are two issues;

1) the run as adminsitrator command, if the uac has been enabled[by default]; this causes a lof grief when it doesn't run scripts thats requires elevation to be able to perform certian tasks. minor tasks/scripts runs okay.

secondly the task schedulre amy not run scripts/tasks, when the

Deny log on as a batch job


This policy setting determines which accounts are prevented from logging on by using a batch-queue tool, the feature in Windows Vista and Windows Server 2008 that is used to schedule and start jobs automatically one or more times in the future. The ability to log on by using a batch-queue tool is needed for any accounts that are used to start scheduled jobs by means of the Task Scheduler.

Possible values:
 User-defined list of accounts
 Not Defined
0
 
LVL 12

Expert Comment

by:S00007359
ID: 37736693
therefore the configured account [in the task scheduler] must have log on as a batch job
rights on that server [either through a local gpo] or a domain gpo.
0
 
LVL 12

Expert Comment

by:Navdeep
ID: 37737052
Does using the batch file helped?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:p-plater
ID: 37739887
The .xlsm, .vbs and .bat files were on a network location - I have shifted them to C:\ExoPrograms\ with not improvement.

My bat script is
@echo off
 echo %date% %time% %username% >> c:\temp\test.txt
 cscript //nologo "c:\ExoPrograms\TFSWebUpdater.vbs" 1>>c:\temp\test.txt 2>>&1
 echo %date% %time% >> c:\temp\test.txt

My .vbs Script
 on error resume next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\temp\Testrun.txt", True)
objFile.WriteLine Now
objFile.Close

strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "WebUpdater.xlsm"
Set objExcel = CreateObject("Excel.Application")
Set objWB1 = objExcel.Workbooks.Open(strExcelFile, False, False)
objExcel.Visible = True
objWB1.Application.Run "Update_web"
objWB1.Close True
objExcel.Quit

Same Problem - It works fine from a short cut but Refuses to run from the scheduler.
Note both the test.txt and the testrun.txt are generated when the task is run from the sceduler but Excel does not open and run the macro.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 37764409
Have you tried making the task run interactively, and also marking it to run with highest privileges?  Since it uses Excel, I dare say you'll need to have someone logged in at the console for it to work, and it would probably have to be the same account being used to run the task.

Regards,

Rob.
0
 

Author Comment

by:p-plater
ID: 37769398
The Task is set to run under my profile.
If I am logged in and try to run it from the task sceduler then Excel refuses to open or run.

What do you mean by making it run interactively?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37769420
Hi, according to here:
http://technet.microsoft.com/en-us/library/cc722152.aspx

It states:
"To make a task run interactively, select the Run only when user is logged on radio button."

So you can try that.

Regards,

Rob.
0
 

Author Comment

by:p-plater
ID: 37769476
OK

When I select the Run only when user is logged Then the task runs fine.
Does this mean I need to stay logged into the Server for it to work?

Any way arround this?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37773986
As far as I know there is no way around this.  An application like Excel must run in an interactive session.  It may work while the server is "locked", if that helps.  Try scheduling the task, then locking the server, and see if it runs.

If your macro doesn't do anything particularly special, you can write to Excel files without using the Excel COM objects, as shown here with Powershell:
http://blogs.technet.com/b/heyscriptingguy/archive/2008/09/15/how-can-i-write-to-excel-without-using-excel.aspx

But if you use formulas in the spreadsheet, these won't be evaluated.

Other than that, I don't think you've got any other option.

Regards,

Rob.
0
 

Author Comment

by:p-plater
ID: 37774260
Is there anyway to set the Sceduler or the VB Script to logon it the user is not logged on?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37774396
No, you can't have a script automate a user logon.  You *could* configure the server to automatically log on when it boots up, but that's up to you.

Rob.
0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

I’m often asked about newer and larger USB drives connected to SBS2008 and 2011 failing Windows Server Backup vs the older USB drives not failing. As disk space continues to grow and drive technology change SBS2008 and some SBS2011 end up with the f…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

759 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

26 Experts available now in Live!

Get 1:1 Help Now