I'm running two PowerShell scripts as scheduled tasks on multiple servers. One script (#1) moves folders from one application to another folder (trash), the other script (#2) restarts services.

Script #1 moves folders by copying them to a destination path and then deleting the folder in the source path. The script works when running manually in a PowerShell console, but when it runs as a scheduled task it doesn't copy folders to the destination path, but it does delete the folder in the source path.

Script #2 restarts MySQL and IIS on two servers, but it usually fails to restart MSSQL on a third server. When I check the scheduled task history, the script runs for 30 minutes before its forced to stop by the scheduler. It restarts a single MSSQL instance so it should only take a few minutes at most.

Both scripts are running with highest privileges with -ExecutionPolicy Bypass and the Start In folder is set to their installation path. I get error (2) for Operational Code in History. This is a file not found message. Each script uses an XML file for settings.

Does anyone have any ideas what I may be missing for the script to run unattended?


  • 2
    Does Script #1 reference mapped drives? If so, try using UNC paths instead. The subsystem that runs the tasks might not be seeing the mapped drives. – Charles Burge Mar 30 '17 at 18:51

What kind of credentials is the scheduled task running with? System and other local accounts will work only on local system. If the script is accessing other computers over the network, the task needs to run with a domain account that has sufficient rights to the other network resources.

At the first/last line of each script, add the following:

start-transcript c:\temp\something.log

That way you can review what errors were thrown during the script execution.

Verify the success of the copy before you delete the source. If the copy is being done with a cmdLet like copy-item look at $?. If it is an EXE like robocopy look at $lastExitCode.

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .