Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 I'm having 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\

EDIT:

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.

share|improve this question
    
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 serverfault.com/questions/266794/… –  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

2 Answers 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.

share|improve this answer
    
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

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.
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.