My PowerShell script won't save to file when running using Task Scheduler; do I need to specify a specific argument?
Solution 1
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.
Solution 2
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.
Related videos on Youtube
EGr
Updated on September 18, 2022Comments
-
EGr over 1 year
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.
-
ProfessionalAmateur over 11 yearsDoes it work if you save the xlsx locally instead of on a network share from Task Manager?
-
longneck over 11 yearsExcel file? As in Excel automation? See serverfault.com/questions/266794/…
-
EGr over 11 yearsI 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 over 11 yearsI 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.
-
ProfessionalAmateur over 11 yearsDo 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?
-
EGr over 11 yearsI 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 over 11 yearsCurrently the script is configured to run as me (MyDomain\Username) and to "Run whether user is logged on or not".
-
ProfessionalAmateur over 11 yearsIf 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. -
MDMarra over 11 yearsDo you also have the box ticked for "Run with highest privs"?
-
EGr over 11 yearsI'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 over 11 yearsI'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.
-
ProfessionalAmateur over 11 yearsNice, 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.
-
MDMarra over 11 yearsI 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.
-
EGr over 11 yearsIt 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 over 11 yearsI 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?
-
Hecter over 11 years@EGr No, but it is wrong to use forward slashes in Windows paths.
-
EGr over 11 yearsAh, that was just a typo in my comment; thanks though!