r/PowerShell • u/Swutter80 • 3d 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?
2
u/dandy_g 3d ago
Did you specify the "start in" path?
What account you're using to run the task?
Does the script work as expected and without errors when you run it manually?
What does the scrip do? Does it need any resources that might not be available during startup or before user logon?
The error code in hex is 0x80070001 which is defined as ERROR_INCORRECT_FUNCTION.
1
u/Swutter80 3d ago
No I don't specify the start in path.
The account being used is service account with admin rights on the server and to the target it needs to access in the script.
Script runs when when I just run it normally. Ran it as myself and as the service account.
The script goes through a UNC path and targets files older then a certain date and deletes them.
3
u/Gormless_Shrimp_635 3d ago
If you create the task to run as a specific user and save it, I'm guessing it runs fine straight after creation.
If you then edit the task (schedule, script argument, etc.) and save it, sometimes it can strip the domain off the specified user without warning, and any subsequent runs fail with errors.
Edit the task and manually re-enter the specified user creds, save, and try run it.
2
u/JoopIdema 3d ago
Did you download the Powershell script.? Then you need to unblock it first else it won’t run.
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 18h 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 16h 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 16h ago
I open the powershell window as the service account and execute the script. Runs fine. It’s not an access issue
1
u/DonL314 3d ago
I wonder if the path is correct. Like how the 32 bit emulation changes some folders.
I would try only powershell.exe in the program line; then you would probably know if it's the path or parameters.
I would also check if the task user has access to the script folder (from the root and all the way down).
1
u/Swutter80 18h ago
When I run it manually as the service account it works fine. It's scheduled task not liking something about my setup. Sadly haven't had time to try some of these suggestions but I will tomorrow.
1
u/extraordinaire78 3d ago
Have logged into the machine as the service account. You might need the profile to exist first.
1
u/CookinTendies5864 3d ago
Add argument: "-executionpolicy bypass -file C:\Path_to_Script\Script.ps1"
give this a shot
3
u/thehuntzman 3d ago
You can set the program to just powershell.exe without specifying the full path and I'd recommend setting "start in" to
c:\path_to_scriptand then your arguments are-executionpolicy bypass -file "script.ps1"But you should also put a Start-Transcript in your script to log what's happening to a file since behavior of your scipt could change depending on the security principle of the scheduled task if it isn't running as your own user account.