"Why Does My PowerShell Function Fail to Copy and Move Files When Scheduled with Windows Scheduler or SQL Server Job Agent?"

That’s not the big step I meant but anyway … to debug your script you should add logging. Are the drives you try to access (E: and Y:) local drives? If they are network drives are they mapped when you run your script? I’d recommend using UNC paths instead of mapped network drives. That’d remove another error prone complexity level.

Since you don’t see console output when you run a script unattended by task scheduler you should write a log file instead of outputting something to the console.

I’d recommend suppressing console output in general for unattended scripts since it slows down your scripts and it does not serve any purpose.

1 Like

Thanks for all your tips … it works now.

Great to hear. :+1:t3: :love_you_gesture:t3:

Why don’t you share the specific solution with the world to help other future readers coming here looking for a solution for the same or a similar issue?

Good idea.
These tips you have given in your answers , shows me the way for solving the problem:
1): Recommending using UNC paths instead of mapped network drives.
2): Since you don’t see console output when you run a script unattended by task scheduler you should write a log file instead of outputting something to the console.

So, When I changed the Destination location to a UNC path and added logging to my scripts (disabled the function part of it with upper and lower curly braces , as you mentioned (“You don’t need a function when it’s about a scheduled task you want to run unattended. Just use a script. You can pass parameters to it just like you do with functions”), Then I created a Batch file containing the script name (saved as the function name , with default values for the Input Parameters, to avoid the need to give it also in the batch file) ,and finally scheduled from Windows Task.

Am working on doing the scripts using your tip :“If you have issues passing parameters to the script you could use a settings file you place next to the script.”, I think it will make scripts more readable and not hardcoded.

Again, I learned a lot from your answers , and I appreciate your help.

1 Like