Solved

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

Posted on 2012-03-18
15
9,276 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

773 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