Why is my PowerShell script failing to run properly in Task Scheduler?

0
2
Asked By CuriousCactus89 On

I'm working with a PowerShell script named `myScript.ps1` that's supposed to execute some native commands and create a text file. Here's a quick rundown of its content:

```powershell
set-content -path "c:temptest.text" -value "hello world"
. 'C:tempmyCliTool.exe'
```

When I set up a task in Task Scheduler, I've tried two different ways in the actions tab:

1. Setting the program/file to `"C:Program FilesPowerShell7pwsh.exe"` and using the argument `-NoProfile -ExecutionPolicy Bypass -command "& { . 'C:tempmyScript.ps1' }"`. This works fine and creates the `test.txt` file.

2. But when I change it to `-NoProfile -ExecutionPolicy Bypass -file 'C:tempmyScript.ps1'`, the script fails to run; the `test.txt` file isn't created, and the native command doesn't execute.

This issue doesn't happen if I run PowerShell any other way. I've been troubleshooting all day, especially since I noticed the Path Environment variable is not available during Task Scheduler calls. I'm at a loss for why using `-file` isn't working as expected. I even tried redirecting errors from the script to a text file but couldn't pinpoint the issue. I'm using pwsh 7.4 on Windows 11.

4 Answers

Answered By HelpfulHarry On

If it helps, I once had to switch to using `Start-Process` instead of running the script directly. It gives more control over how it's executed and lets you redirect output and errors easily, which might help you debug any further issues.

CuriousCactus89 -

I’ll definitely consider that for future scripts. Thanks!

Answered By ShellSavant On

You might want to check if the `set-content` command itself is working properly. Does that command create the text file when run manually? If it doesn’t, the issue may not be with the Task Scheduler at all. Try running that part of the script independently to diagnose.

CuriousCactus89 -

I didn’t check that specifically. I’ll do that now!

Answered By TechWhiz101 On

It sounds like you're running into issues with how the script path is treated in different contexts. In your `-File` command, have you tried wrapping the path to `myScript.ps1` in double quotes? I had the same problem, and just doing that allowed everything to run correctly! Double quotes are key here for Windows to recognize the path properly, unlike single quotes.

ScriptSavvy -

It worked for me too! Thanks for pointing that out!

PowerUser99 -

Yes! Double quotes are essential for the path. Glad you figured it out!

Answered By CodeCracker On

I ran into a similar situation. It’s not really a Task Scheduler issue; it's often about how paths are handled in different contexts. Make sure to explicitly set your working directory or use the appropriate user for the task. Sometimes, running under the system user can cause issues if the script relies on certain paths that it can't access.

CuriousCactus89 -

That makes sense! I’ll see if that helps with my other scripts too.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.