3

I have a script that downloads a temporary Excel file, copies parts of it to a new file, and saves it to a specific location on the network.

The problem is that the new file is never created/saved. If I run the script locally (through cmd.exe, PowerShell, or PowerShell ISE), it WILL save the file locally, or to the network. If I try running the script via a schedule or on-demand via Task Scheduler, the temporary file is created, but the final document is never created or saved. Is there a specific argument I need to pass, or anything I could be doing wrong? This is the command I'm currently using:

    powershell.exe -file C:\path\to\my\powershell\script\thescript.ps1

Since it calls environment variables, and other variables relative to the scripts positon, I also set "Start in" to

    C:\path\to\my\powershell\script\

I have tried using

    \\MYSERVER\Path\To\Directory\file.xlsx

as the location for the file on the network (as suggested here), but this does not work either.

EGr
  • 575
  • 3
  • 12
  • 29
  • Does it work if you save the xlsx locally instead of on a network share from Task Manager? – ProfessionalAmateur Sep 27 '12 at 21:11
  • 1
    Excel file? As in Excel automation? See http://serverfault.com/questions/266794/when-ran-as-a-scheduled-task-cannot-save-an-excel-workbook-when-using-excel-app?rq=1 – longneck Sep 27 '12 at 21:49
  • I moved to another computer at work, and starting having this problem again. I tried the second answer in this link, and it worked! I just needed to create a "Desktop" folder in a specific location. Thanks! – EGr Dec 11 '12 at 18:59
  • My problem when trying to get this to work (https://superuser.com/a/1248413/74576) was that I needed to use absolute paths instead of relative paths. – Ryan Sep 08 '17 at 00:10

2 Answers2

2

The scripts works when you are running it because YOU have rights to the destination location. By default, a scheduled task runs as the local system user.

You should create a new, dedicated user in your domain for this purpose, give it exactly the rights it needs to complete this task (and no more), and configure your scheduled task to run as that user.

longneck
  • 22,793
  • 4
  • 50
  • 84
  • Currently the script is configured to run as me (MyDomain\Username) and to "Run whether user is logged on or not". – EGr Sep 27 '12 at 21:02
  • Do you also have the box ticked for "Run with highest privs"? – MDMarra Sep 27 '12 at 21:11
  • 1
    I've tried with, and without that; neither worked. I just tried to do it "only when user is logged on", and that worked! It must have to do with the fact that I had tried doing it whether I was logged on or not. – EGr Sep 27 '12 at 21:15
  • I'll have to look and see what is wrong when doing it the other way. I'll have to leave myself logged on until I figure this out. – EGr Sep 27 '12 at 21:21
  • I don't believe that your user environment is loaded when you have it run when you're not logged in. Is your script using environmental variables specific to your user? If so, you should set them at the beginning of your script so that they're available for that session. – MDMarra Sep 27 '12 at 23:16
  • It doesn't have any environment variables, but I have a few global variables. One in particular gets the path of the script: "$global:scriptpath = split-path $script:MyInvocation.MyCommand.Path" – EGr Sep 28 '12 at 12:25
1

How do you have your parameters specified in the powershell script (can you post a snippet of it with the parts in question)? You usually need double quotes around parameters when calling it from Task Scheduler.

Does the script work if you call it from the Powershell command prompt?

This question might help a little. Not exactly the same thing but I bet its a similar solution.

Here is another TechNet article that may help with this issue. Brief summary:

Found the answer to this - enclosing my entire arguments in double quotes and then my parameters in single quotes ensured that the correct values were being passed to my parameters.
ProfessionalAmateur
  • 917
  • 5
  • 17
  • 26
  • I actually don't pas any parameters into the script, it is just run by calling it. The script is able to be run from the Powershell command prompt. It can be run from the Powershell command prompt, Powershell ISE, and cmd.exe. – EGr Sep 27 '12 at 20:49
  • Do you specify where the file needs to be saved to in your script as a parameter? Those parameters may have to be escaped better. Can you post your script? – ProfessionalAmateur Sep 27 '12 at 20:50
  • I can't post my script, but this is how I declare the global variable that I use to specify the location where the file will be saved: $global:replocation = "S:\SharedFolder\OtherFolder\AndAnother\Reports\report.xlsx" – EGr Sep 27 '12 at 20:59
  • If you can't post it, I would try to create a dummy script and start using `write-host $global:replocation` to narrow it down and find out which variables/parameters are being lost in the script. – ProfessionalAmateur Sep 27 '12 at 21:07
  • Ah! It seems a script that just calls write host to that variable doesn't work this way either! I'll have to check and see what is causing this... – EGr Sep 27 '12 at 21:16
  • Nice, that is probably it. I just went through the same thing where it worked from the command prompt, but the values were lost via Task Scheduler. Good luck, let us know what ends up fixing it. – ProfessionalAmateur Sep 27 '12 at 21:36
  • I still haven't had a chance to fix this issue. Could this issue be caused because I am referencing the network location as "S:/the/directory" ? Is it wrong to use the letter of the drive? – EGr Oct 02 '12 at 20:54
  • @EGr No, but it *is* wrong to use forward slashes in Windows paths. – Skyhawk Oct 02 '12 at 21:46
  • Ah, that was just a typo in my comment; thanks though! – EGr Oct 03 '12 at 15:29