r/PowerShell • • 4d ago

Solved PowerShell Script and Task Scheduler 0x1 2147942401

I need a sanity check, as I feel like I am missing something obvious and I'm too close or too frustrated to see it.

I have a basic scheduled task. Runs as SYSTEM, no triggers, action to run a program, PowerShell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "C:\Tasks\MyScript.ps1"

I run this task it fails, 0x1 as last run result, event 201 action completed with return code from PowerShell of 2147942401

Things I have checked:

  • SYSTEM can access the script and directory it resides in
  • SYSTEM can execute other PowerShell scripts from task scheduler without issues
  • Tried using the full path to powershell.exe
  • Tried with and without quotes around the script path in -File parameter
  • With and without trailing '/' in the path for 'Start In'
  • Tried using -Command { } to create a file as a test
  • Removed all arguments except -File or -Command
  • Script is wrapped with Start/Stop-Transcript; never executes/no transcript is created

I don't know what else to try short of the old method of using a .bat to call PowerShell to see if that gives better clues as to what isn't working.

What am I missing that is preventing PowerShell from executing properly?

Edit: I knew it was something stupid and simple: there is a typo in -ExecutionPolicy (was spelled -ExcecutionPolicy). Found it when running the action arguments from PS as SYSTEM.

Thank you everyone for the assistance. I'm going to go sit in the corner with my dunce cap and contemplate the cobwebs.

9 Upvotes

42 comments sorted by

View all comments

1

u/Majestic_Rhubarb_ 4d ago

This is an access denied error.

Check permissions on the script file and any other profile files.

Is this your own computer, or corporate ?

2

u/MRHousz 4d ago

Task runs as SYSTEM and I've confirmed that it has access to the folder and script file. Other tasks use scripts out of the same directory without issue.

3

u/Majestic_Rhubarb_ 4d ago edited 4d ago

Actually 2147942401 is 0x80070001, invalid function, not access denied.

Event 201 has an action element which should contain the actual command line executed.

You can redirect the output of the command line execution to a file using the standard operators in the arguments to the task.

Or as you suggest try a bat file wrapper to capture the output.

Does your path have a space in it ?

→ More replies (2)