2

I have such script (here simplified):

while ($true)
{
    Write-Host "Looping..."
    Write-Host "Looping..." 6>> trash.txt
    Start-Sleep -s 1
}

when I run it directly it works, but when I run it in the background:

Start-Job { .\sleeper.ps1 }

for a second it is seen as Running but shortly after as Failed and indeed file "trash.txt" is not created at all, so even one iteration is not executed.

What is wrong here?

astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • Try using the full path to sleeper.ps1 and see if that makes a difference. – GregHNZ Sep 27 '19 at 06:27
  • 1
    This may have to do with a few things. First, why are you using `Write-Host` for this? Is there a strong reason why you are using `Write-Host` instead of something like `Write-Output` / `Write-Verbose` / `Write-Error` / `Write-Warning`? If you aren't sure why, checkout something like https://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/ for more info, along with `help about_Redirection`. Also, if running a script, use `Start-Job -FilePath ./script.ps1` and not `{ }`, which I believe is using `-ScriptBlock` – ScriptAutomate Sep 27 '19 at 06:29
  • @ScriptAutomate, thank you a lot. So with all your tips I now can run the script, but it does not dumpy anything to a file. Currently I use `Write-Output "Looping..." | Out-File -append trash.txt`. It works when executed directly (i.e. in foreground). – astrowalker Sep 27 '19 at 07:49

1 Answers1

1

I think the main issue is around the $PWD and -FilePath param, but I will list some info on Write-Host too:

  • Start-Job should be run with the -FilePath parameter. This is because {} is a ScriptBlock object, which is by default taken by the -ScriptBlock parameter, which you do not want. Example solution to that line: Start-Job -FilePath ./script.ps1
  • The $PWD or present working directory is, by default, the home directory / user profile of the current user when executed in a PowerShell job (Linux: $HOME // Windows: $Home/Documents). You can test this by executing a job simply with that variable (it may be $ENV:PWD on Windows?). Either way, it is likely not the same as the directory you are executing this in. trash.txt is being made and appended to, but it is being made in a different directory than your current directory. You will want your script to explicitly include an absolute path to the file being created, or give the script parameters that allow you to input the path at execution. Here is another StackOverflow article where a user had similar struggles, with two good solutions where one uses $args in the script and another uses the -InitializationScript parameter of Start-Job to set the $PWD: PowerShell Start-Job Working Directory
  • Often, Write-Host is low-priority as a selected output vs. using Write-Output / Write-Verbose / Write-Warning / Write-Error. More information about this can be found here: https://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/ - though, newer versions of PowerShell have added an information stream which I believe may make Write-Host output more accessible. More information on streams can be found with help about_Redirection
ScriptAutomate
  • 980
  • 9
  • 17