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

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?
p-platerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NavdeepCommented:
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
RobSampsonCommented:
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
p-platerAuthor Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

RobSampsonCommented:
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
S00007359Cloud Engineering OfficerCommented:
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
S00007359Cloud Engineering OfficerCommented:
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
NavdeepCommented:
Does using the batch file helped?
0
p-platerAuthor Commented:
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
RobSampsonCommented:
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
p-platerAuthor Commented:
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
RobSampsonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
p-platerAuthor Commented:
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
RobSampsonCommented:
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
p-platerAuthor Commented:
Is there anyway to set the Sceduler or the VB Script to logon it the user is not logged on?
0
RobSampsonCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.