Having Trouble Running Powershell Scripts with Task Scheduler

Mikros476 45 Reputation points
2023-04-20T20:19:53.45+00:00

I wrote a simple PowerShell script to back up a certain folder from a server to an external NAS, which is mapped as the N: drive. $sourcePath = "D:\SomeLocation\Folder" $destinationPath = "N:\BackupData" Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -Force

  • The credentials for the NAS are stored in Windows Credential Manager
  • The script, if run directly on its own manually by me, is able to perform the backup correctly and I see the files.
  • The script is set to run as an administrator account, with highest privileges, whether logged in or not, and configured for Win Server 2019
  • However, when I call it with Task Scheduler (even clicking Run manually rather than waiting for the scheduled time), it doesn't seem to do anything. Looking at the history, it says it successfully completes it though, oddly.
  • For the Program/script field I put the path to the PowerShell executable "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe**"**
  • For the Add arguments field, I enter the path to the location of the script itself, for example in my Documents folder. I also later tried adding a -ExecutionPolicy Bypass to the arguments field, to no avail. Adding or removing quotes made no difference, as expected.
  • I didn't think the Start In field was necessary in this case, but I have tried entering the location of the folder containing the script, or even of the folder that needs backing up, but none of it made a difference.
  • As a workaround I also tried creating a .bat file and entering cmd.exe as the Program/script field and the location of the .bat file in the argument field C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Users\Administrator\Documents\BackupData.ps1" 1>C:\Windows\temp\BackupData.log 2>&1 But unfortunately this also didn't work. I also don't see log files for it. Not really sure what's going on. Any help would be appreciated. Thanks.
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,593 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,421 Reputation points
    2023-04-24T22:51:33.3733333+00:00

    Powershell works the other way. You have to specify the -NoExit switch to keep it running after executing a command or a .ps1 file.

    Your main issue is one that we see quite frequently. User writes a script. User runs script just fine in desktop session. User tries to run in task scheduler (or startup) but doesn't get expected results. User does not include any error handling or logging in the code and has no visibility into what the unattended script did. User asks us what is wrong with the task scheduler.

    The simple solution is to include Out-File cmdlets in the script to write to a log file. If you need more detail, PS's Start-Transcript/Stop-Transcript might provide more info. Write out script start and end times. Put a try/catch around the main code block and write any error to your log file.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-5.1

    If the log/transcript files aren't being created, then PS itself must be running into some kind of fatal error. You then need to capture stdout and stderr from PowerShell.exe to see the error. That's where the bat file comes into play.

    For low level debugging use Sysinternal's Process Monitor to trace what Powershell.exe does.

    https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    The task scheduler is perfectly capable of running powershell.exe directly. Once you implement error handling and logging in your script, you should be able to remove the bat file.

    3 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MotoX80 34,421 Reputation points
    2023-04-20T21:17:02.1333333+00:00

    Create a folder C:\Logs and grant Everyone full control. Change C:\Windows\temp\BackupData.log to C:\Logs\BackupData.log

    In the bat file add a first statement that shows the run time.

    @echo Task is starting %date% %time%
    
    

    In the Powershell script add Test-Path cmdlets for both $sourcePath and $destinationPath When you map a drive, this is only for your logon session. If you are using a different account, you will need to map a drive for that account too. Also the task scheduler just does a user impersonation, not a full logon. You probably don't have an N drive visible to the script. Log on to the desktop with the account that the task runs as. Use credential manager to verify that it sees the saved credentials for the NAS. In that case try using the UNC notation \NASserver\sharename

    1 person found this answer helpful.

  2. Rich Matheisen 46,811 Reputation points
    2023-04-20T21:24:07.1+00:00

    Try using the UNC name instead of the mapped drive letter ("N:"). IIRC, a mapped drive is associated with a user interface, and a background task doesn't have one.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.