Robocopy script not running from task scheduler



  • Ok, so we have a simple robocopy script that copies over a db file from a remote server to the file server. When the script is run manually, directly from powershell it works without trouble but when the script is run from task scheduler it doesn't complete. It just shows as running and it eventually timesout according to the max time set to run in task scheduler. It actually doesn't create the robocopy log file so no idea what is really happening.

    OS is Windows Server 2012R2
    Script:
    robocopy $originDirectory $destinationDirectory /mir /V /log+:C:\folder\script.log

    Security options for the script are set:
    security-options.png

    Trigger is:
    Program: C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
    Arguments: -Execution Policy Unrestricted -File "C:\folder\script.ps1"
    Start in : C:\folder\

    Share permissions are properly set since the script being manually run works properly unless I am missing something.

    Credentials for the remote share are stored properly in credential manager.

    Any idea what I am missing in order for the script to properly be run from task scheduler



  • @Romo said in Robocopy script not running from task scheduler:

    Arguments: -Execution Policy Unrestricted -File "C:\folder\script.ps1"

    Is the path to the remote server available to the account the script is running under? ie; the system account or another? You may have to net use the path with credentials in the script. Just my first guess.....



  • @JasGot Same account currently as the user running the script manually. Also forgot to mention it had been working before without adding net use and the UNC until someone else tried to make a modification.



  • @Romo said in Robocopy script not running from task scheduler:

    @JasGot Same account currently as the user running the script manually. Also forgot to mention it had been working before without adding net use and the UNC until someone else tried to make a modification.

    Ah! A helper!

    Must be a syntax error early in the file.



  • @JasGot In the script file you mean?



  • It does run without trouble when running directly though, so not sure about a syntax error.



  • @Romo said in Robocopy script not running from task scheduler:

    It does run without trouble when running directly though, so not sure about a syntax error.

    Is the server it is running on a DC?

    Does it fail to run if logged in or not logged in?



  • @JasGot Yes, it is actually the DC server. From task scheduler I have tried it logged in as the same account that is running the script and from a different user account with the script user not logged in at all.



  • Have you tried using Bypass instead of Unrestricted?





  • @Romo said in Robocopy script not running from task scheduler:

    @JasGot Same account currently as the user running the script manually. Also forgot to mention it had been working before without adding net use and the UNC until someone else tried to make a modification.

    If it was working before and then stopped working when nothing was changed, that smells like a Windows update messed things up.



  • @Obsolesce Would have to agree there. I do this on many machines here and at first glance it sure looks right. I;m not running from a dc though



  • @Obsolesce said in Robocopy script not running from task scheduler:

    @Romo said in Robocopy script not running from task scheduler:

    @JasGot Same account currently as the user running the script manually. Also forgot to mention it had been working before without adding net use and the UNC until someone else tried to make a modification.

    If it was working before and then stopped working when nothing was changed, that smells like a Windows update messed things up.

    Yeah, it could be a possibility. Something in the task was changed though but not sure exactly what was changed. We were told a couple of days after it had stopped working.

    What I really find strange is that the scripts works just fine when manually run. We must be missing something somewhere that makes the script fail on task scheduler.

    Maybe when running through task scheduler it can't access the credentials in Credential manager until something is executed beforehand??



  • Ok finally something new, apparently something is wrong with the destination folder as I am getting:
    Screenshot from 2019-04-02 19-00-38.png

    So through Task scheduler it cant write to the destination folder for some reason.



  • For Google: "ERROR 19 (0x00000013) Accessing Destination Directory. The media is write protected."



  • @Romo said in Robocopy script not running from task scheduler:

    Ok finally something new, apparently something is wrong with the destination folder as I am getting:
    Screenshot from 2019-04-02 19-00-38.png

    So through Task scheduler it cant write to the destination folder for some reason.

    Does the destination path have enough storage? If the path is an external drive, make sure it's not showing as read-only in Disk Management.



  • @Romo said in Robocopy script not running from task scheduler:

    Maybe when running through task scheduler it can't access the credentials in Credential manager until something is executed beforehand??

    If you are running your script as SYSTEM, the credentials need to be put into Credential Manager as SYSTEM.

    For example, use PSEXEC to run powershell as SYSTEM, then you can add credentials to Credentail Manager and call them from PowerShell using the CredentialManager PowerShell module.



  • @Obsolesce This kind of what I do. I had too many problems with credentials in windows and powershell so I just started psexec within powershell to bypass a lot of that trouble. Works better for me anyway.



  • @jmoore said in Robocopy script not running from task scheduler:

    @Obsolesce This kind of what I do. I had too many problems with credentials in windows and powershell so I just started psexec within powershell to bypass a lot of that trouble. Works better for me anyway.

    I meant that in the scenario that you use a Scheduled Task to run a PowerShell script as SYSTEM... in that case, when it looks for credentials in Credential Manager, itwon't find them, unless you add credentials to Credential Manager AS the SYSTEM account, which means you need to be running PowerShell interactively as SYSTEM.

    What I've been doing lately is working with Azure Automation PowerShell Runbooks, which has it's own Credential Manager.... which basically works identically to the CredentialManager PS module. So if you use that you don't even need a Windows box. Also, you can simply have a parameter in your PowerShell Runbook to accept webhook that executes the runbook... which is freakin sweet! It's great for building API flows between Azure/Intune/etc and 3rd party services!



  • @Obsolesce Oh thats interesting. I'm not familiar with it. I will look at that when I get time, thanks.



  • @EddieJennings yes destination has enough storage, shares are writable as well as running the script manually works without an issue.



  • @Obsolesce Haven't tried it by running it directly as SYSTEM, it has been run as a user and checked to be run with the highest privileges. As it was when it was working before.