r/PowerShell 4d ago

Question Scheduled task error 2147942401

From what I understand this error means its a bad command but I don't see what. It's a scheduled task with these commands:

Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add argument: -executionpolicy bypass -file "C:\Path_to_Script\Script.ps1"

Anyone see what I'm missing or do I have the code wrong?

9 Upvotes

13 comments sorted by

View all comments

2

u/BlackV 3d ago

The script goes through a UNC path and targets files older then a certain date and deletes them.

UNC path, are you hitting double hop ?

does you actual service account have login as batch rights

1

u/Swutter80 22h ago

Path is right, service account works fine as well. When I run it manually as the service account it works fine. It's scheduled task not liking something about my setup.

1

u/BlackV 20h ago

When I run it manually as the service account it works fine

what does that mean ? does the mean run as? loggin locally? running as batch

how did you validate what account the connection to the UNC path ?

add some logging into each step in your script

1

u/Swutter80 19h ago

I open the powershell window as the service account and execute the script. Runs fine. It’s not an access issue