Avatar of p-plater
p-plater
Flag for Australia asked on

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?
VB ScriptWindows Server 2008SBS

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
Navdeep

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]
RobSampson

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.
p-plater

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
RobSampson

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.
Roshan Mohammed

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
Roshan Mohammed

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Navdeep

Does using the batch file helped?
p-plater

ASKER
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.
SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
p-plater

ASKER
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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
p-plater

ASKER
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?
RobSampson

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.
p-plater

ASKER
Is there anyway to set the Sceduler or the VB Script to logon it the user is not logged on?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RobSampson

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.