0

At first I tried using the following as a command for a scheduled task (see the full dump of the task below)

 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command C:\Users\knegyesi\Documents\wsl2-network.ps1 *>C:\Users\knegyesi\AppData\Local\Temp\log.txt

which does start however wsl hostname -I comes back empty while it perfectly works from command prompt. Using the full path to wsl.exe doesn't change anything.

I then read to use a cmd or vbs file instead of running the PS1 directly which I did. That, however, doesn't run at all.

I have simplified my case as much as I possibly could. I have reviewed similar questions and either they offer no solution or the solution doesn't work.

I have created a cmd file:

echo foo > %TEMP%\log.txt

I've created a scheduled task

Folder: \
HostName:                             CF3N2Z2
TaskName:                             \wsl2-network
Next Run Time:                        N/A
Status:                               Ready
Logon Mode:                           Interactive/Background
Last Run Time:                        11/15/2021 2:25:52 AM
Last Result:                          0
Author:                               APOLLO\knegyesi
Task To Run:                          C:\Users\knegyesi\Documents\wsl2-network.cmd
Start In:                             N/A
Comment:                              N/A
Scheduled Task State:                 Enabled
Idle Time:                            Disabled
Power Management:                     Stop On Battery Mode, No Start On Batteries
Run As User:                          SYSTEM
Delete Task If Not Rescheduled:       Disabled
Stop Task If Runs X Hours and X Mins: 72:00:00
Schedule:                             Scheduling data is not available in this format.
Schedule Type:                        At system start up
Start Time:                           N/A
Start Date:                           N/A
End Date:                             N/A
Days:                                 N/A
Months:                               N/A
Repeat: Every:                        N/A
Repeat: Until: Time:                  N/A
Repeat: Until: Duration:              N/A

And then I did schtasks /Run /TN "wsl2-network"

The Task Scheduler GUI reports success. However, the log file is not created. Manually running the script works. I tried adding @echo off to make sure the program has no output.

I am completely stuck, I really need this script to run at boot and not at login :(

chx
  • 3,903

1 Answers1

0

Two comments:

First, as noted above by DavidPostill, you shouldn't assume the value of any environment variable inside a scheduled task, because this is a limited environment.

Second, echo is a command of the Command Prompt (CMD), which the scheduled task is not running.

It's better to use for a scheduled tasks a .bat file, which will be executed inside an implicit CMD environment. Remember to use full paths for every executable file that is called, do not assume the same PATH as in your login.

harrymc
  • 480,290