WSL: In Window 10 with WSL installed, a Task Scheduler task setup to run at Startup/Logon which intiates a PowerShell script which executes the bash command fails.

Please fill out the below information:

  • Your Windows build number: (Type ver at a Windows Command Prompt) Microsoft Windows [Version 10.0.17763.253]

  • What you’re doing and what’s happening:
    In Window 10 with WSL installed, a Task Scheduler task setup to run at Startup/Logon which intiates a PowerShell script which executes the bash command fails. This is the PowerShell script:

#power shell file saved as autostartsshd.ps1
bash -c 'sudo service ssh start'

Note: Running this with powershell as Administrator works fine. If fails only when run as a task via TaskScheduler

  • What’s wrong / what should be happening instead: But running from Task Scheduler at log on throws this error:

bash : The term ‘bash’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\ssh_startup\autostartsshd.ps1:2 char:1

  • bash -c ‘sudo service ssh start’
  •   + CategoryInfo          : ObjectNotFound: (bash:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

Note the reason it cannot find bash is that bash.exe is found only in the 32 bit version (c:\Windows\System32\bash.exe) and it looks like the path for TaskScheduler does not include 32bit libraries by default in Windows?

  • Workaround: Create a symlink to 32bit bash.exe in SysWOW64
PS C:\Windows\system32> cd ..\SysWOW64\
PS C:\Windows\SysWOW64>  New-Item -ItemType SymbolicLink -Name bash.exe -Target c:\Windows\System32\bash.exe

Now the TaskScheduler task executes successfully and the ssh service is started as instructed.

Can this PATH issue be fixed in a future release, so that this unnecessary workaround can be avoided?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (4 by maintainers)

Most upvoted comments

@rodrymbo Confirming that using named distro exe to launch works, Specifically I am using Ubuntu 18.04 LTS. This works:

#power shell file - using native distro exe to access WSL
ubuntu1804.exe run "sudo service ssh start"

Thank you.

Another workaround might be to use the distro-named .exe, which is probably in a “normal” directory. For example, ubuntu1804.exe can start scripts on its command line (rith appropriate syntax), is usually on the Path, and doesn’t require the sysnative trick to work, I don’t think, usually. Your distro may vary.

It’s not entirely clear that it’ll fail: sysnative is an escape route for 32-bit processes to access the native System32 directory (which on 64-bit windows actually contains 64-bit binaries.)

Since it looks like the task being scheduled is launched with filesystem redirection turned on, and perhaps even in 32-bit mode itself, sysnative should work.

The issue isn’t exactly that system32 isn’t in the path; it’s that system32 is redirected to syswow64 for a 32-bit process.

What about fully qualifying the path to bash.exe or wsl.exe (c:\Windows\System32\bash.exe).