Solved

Using a PowerShell script to refresh an Excel Document through Windows Task Scheduler

Posted on 2016-11-21
3
103 Views
Last Modified: 2016-11-21
I have written a simple Windows PowerShell script that opens an existing Excel document and refreshes all the data contained within;
$app = New-Object -comobject Excel.Application
$app.Visible = $True
$wb = $app.Workbooks.Open("C:\batch\Test.xlsx")
$wb.Name
$wb.RefreshAll()
$wb.Save()
$wb.Close()
$app.Quit()

When I run this script from Windows PowerShell ISE it works just fine, when I right click the .ps1 file and select Run with PowerShell it also works just fine. When I set this script to run in Windows task scheduler and select 'Run only when user is logged on' I'm able to successfully run it from Windows Task Scheduler. However, when I select 'Run whether user is logged on or not' it no longer runs, even if I select 'Run with highest privileges'.

Ideally I would like for this script to run regardless of whether anyone is signed on to this server, but doing some online research it seems like it is well documented that this is a difficult task to accomplish.

My online research did yield the following solution, that if I create these two foldesr;
C:\Windows\System32\config\systemprofile\Desktop
c:\windows\syswow64\config\systemprofile\desktop
it should allow the script to succeed, however, this has not seemed to work for me. Any advice or guidance would be most appreciated. Thanks in advance!
0
Comment
Question by:OCCU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41896422
It may work if you change to use this.
$app.Visible = $False

Open in new window


When 'Run whether user is logged on or not' is selected, you can't have any GUI elements or it's likely to fail.

You could add commands to your script to write to a log file, so you can see how far through the script it gets.  Error handling might have to be added if execution doesn't actually hang.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 41896426
I'm not sure how you set up your task scheduler, so here's what I found:

https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
0
 

Author Closing Comment

by:OCCU
ID: 41896539
It seems to be working now!
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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