Excel vba script hangs when run from Task Scheduler on Windows Server 2008 - excel

I have a vbs script that opens Excel and runs a vba script. It works fine on Windows 7, both when run manually and as a task in Task Scheduler. It works fine when run manually on Server 2008 but when I run it as a scheduled task, it hangs. I added some rudimentary logging to the vbs file and it appears to hang as soon as the vba script is run. Excel is (apparently) opened fine.
I have already:
Chosen the correct directory for "Start in"
Chosen "Run with highest privileges"
Added the "Desktop" folder to C:\Windows\SysWOW64\config\systemprofile
Made sure the user that the task is set to run as has full access to all of the files and folders touched by the script.
Tried all possible variations of this: Task Scheduler running but not finishing or working properly VBscript
Set to "Run whether user is logged in or not"
(note: before adding the desktop folder the task "ran" successfully but didn't actually do anything it was supposed to)
Any ideas?

Related

How To Automatically run a powershell cmd on a specific folder on windows 10 start up after a delay (30s)

We are using web project unfinished, and everytime the computer start, we need to go to:
c:/wamp64/www/
In this folder we have a folder "projectname", we have to shift right click , and open powershell windows here.
then we have in the terminal: PS C:\wamp64\www\projectname> (and here we have to write "node server.js") and enter.
This have to be done once wamp is open, this is the reason of the delay.
How can we do that automatically at windows start up after 30s ? that way we won't have to do that every time we reboot a computer.
thank you for your help
I would use the task scheduler for that purpose.
Open Task Scheduler by pressing “Windows+R” and then typing “taskschd.msc” in the window that opens. Then take the following steps:
Click “Create a task” and enter a name and description for the new task. To run the program with administrator privileges, check the “Run with the highest privileges” box.
Switch to the Triggers tab and click the “New…” button. Here you can specify the conditions that trigger the task to be executed. For example, you can have it executed on schedule, at logon, on idle, at startup or whenever a particular event occurs.
Choose At log on then set it to run for Any user and set the delay to 30 seconds
Navigate to the “Actions” tab, and click “New…”.
To schedule the PowerShell script, specify the following parameters:
Action: Start a program
Program\script: powershell
Add arguments (optional): -File [Specify the file path to the script here]
References
How to automate powershell scripts with task scheduler

Scheduled task not run using x86 version of Powershell

I have written a Powershell script and it is saved in a .ps1 file. It only works with the 32-bit version of the Powershell that is located in
%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe
The script works when I run it manually but it is not run via Windows Task Scheduler. I am running the task as the same user that runs the script. In the Action part of my task I put the above address as Program/script and I write the full path of my .ps1 file as Add Arguments (optional). But it does not seem to work. I have also tried with putting the parent folder of my .ps1 file as Start in value to no avail.
How can I tell Task Scheduler to run my Powershell script using the 32-bit version?
UPDATE: I have to add here that my script actually opens an Excel file, refreshes it and then closes it. I know that using Excel in a non-interactive environment is a bad idea. But I still don't know if this is the reason my script is not run.
Highly suspect Excel is the reason this appears not to work. Have your script do something non-Excel (e.g. create file) and check if this part was executed fine
Two major gotchas I've come across when automating Excel:
Create empty folders if they don't exist (excel automation bug)
Ensure DCOM security settings are configured to allow Excel to run. This is still required if you are running task as same user who manually runs script.
When DCOM permissions are not set correctly and running the script as an automated task, you will get the below error. Saw this as session was transcribed, and transcription output to text file.
New-Object : Retrieving the COM class factory for component with CLSID
{00024500-0000-0000-C000-000000000046} failed due to the following
error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED)).
You could adopt your script to determine the current powershell version and invoke the same script with the 32bit version if necessary. Put these lines on the top of your script:
# ensure the script is running with the 32bit powershell
if ($env:Processor_Architecture -ne "x86")
{
$psx86 = Join-Path $env:SystemRoot '\syswow64\WindowsPowerShell\v1.0\powershell.exe'
& $psx86 -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
exit
}
Note: You can append parameter to the powershell invoke in case your script requires them.

Powershell script not running on Task Scheduler

I have a PS script that opens Excel (Com Object), processes a bunch of information, re-saves, and then sends some critical information via e-mail. The script runs great, and when I run it from the Run Console, it works great as well. However, when I schedule it as a task in the Task Scheduler it is not working properly. The task seems to "successfully run" every single time, but I do not get the output e-mail that I am supposed to get. I have run many other PS Scripts without a problem using the same configuration in the task scheduler. Could this have something to do with opening Excel as part of a script scheduled? Any thoughts are welcome.
I solved this issue using the answer from #briantist in PowerShell script won't execute as a Windows scheduled task, but I wanted to isolate exactly which switch was solving the problem.
It had nothing to do with -ExecutionPolicy, -Noninteractive, -NoLogo, -NoProfile or any other system privilege, user account running the script, etc.
Just needed to add -File in front of the script path in the Task Scheduler > Actions > Arguments field. Without this switch PowerShell was launching and the task history was showing Action Completed, but the script was not executing.

Scheduled Task Not Creating Excel File

I developed an application which grabs some data from our server, spits it out into an excel sheet, and emails that sheet to specific users. I needed this application to launch every 30 minutes so I setup a scheduled task on our server to run this application. What I can't figure out is this: The application runs (I can see that the data is being collected from our database (marked as hit in a [Processed] column)), but it will not create the excel file, and consequently does not send the email.
I need to figure out why so this process can work for me. A few things to note:
1.) I am an admin for this machine. (double checked my authorities)
2.) Excel IS installed on this machine.
3.) The scheduled task doesn't work, but if I manually run the program it executes perfectly.
4.) Proper versions of .Net are installed.
5.) Other scheduled task exist that do similar operations and have no issues.
The server is 2008 Enterprise.

powershell IQY dump script fails

I'm currently working on automating a powershell script to make a dump of a small DB.
The database is accessible through a company Sharepoint and I am leveraging an IQY file exported from said Sharepoint to read the database contents.
When I run the script manually (i.e. calling it from the command line), it works with no issues, but when I try and start the script from Task Scheduler, the following exception is raised:
System.Reflection.TargetInvocationException
the line that is generating the exception is:
$iqy = $xl.Workbooks.Open($query, 2, $true)
Where $xl is a new Excel.Application ComObject, created correctly and $query is a string containing the full file path of the iqy file.
As for my configuration, I'm trying to run my script on a Windows Server 2008R2, using a local administrator user (same user that is supposed to launch the scheduled script), which is also authorized to access the Sharepoint. I'm running Powershell 3.0. I'm not loading any Sharepoint-related snapins.
The only answers I found poking around hinted at granting "Trust" to the IQY file location and the Sharepoint itself in Excel. That did not help.
The Exception also seems to point at a sharepoint issue, but since the script runs fine when started manually, I cannot understand how it could be anything to do with Sharepoint.
Am I missing some intricacy of the Task Scheduler?
Any help would be greatly appreciated.
Never mind, I seem to have found an answer.
If anyone is encountering the same issue, it appear the Excel Com Object has a bug that does not allow it to run from the Task Scheduler if you set it to run regardless of whether the user is logged in.
To circumvent that create the following 2 folders on the machine where the script is supposed to run:
(32Bit, always)
C:\Windows\System32\config\systemprofile\Dektop
(64Bit)
C:\Windows\SysWOW64\config\systemprofile\Desktop
After creating the folders, it worked as expected.
Source: http://social.technet.microsoft.com/Forums/windowsserver/en-US/aede572b-4c1f-4729-bc9d-899fed5fad02/run-powershell-script-as-scheduled-task-that-uses-excel-com-object?forum=winserverpowershell

Resources