简体   繁体   中英

how can I pipe a powershell call command

I'm porting some pytest linux bash scripts to windows powershell.

Without the pipe stage at the end script runs fine. But I need a complete $outputlog for next stage parsing. Script runs fine and output to what is should to file in all examples but it hangs at the end. never continue without a ctr+c.

If I add a pipe to the end, the call never returns and hangs until I quit it. $outputlog is created fine:

&"$exe" -s --capture=no --log-cli-level=$loglevel \
--webhost=$testenv --durations=0 --tb=short .\tests --html=$testenv.html \
| Set-Content $outputlog

if I user Transcript, the $outputlog is empty except for the transcript header and footer:

Start-Transcript -Path $outputlog
&"$exe" -s --capture=no --log-cli-level=$loglevel \
--webhost=$testenv --durations=0 --tb=short .\tests \
--html=$testenv.html
Stop-Transcript

This one writes nothing to console but all to $outputlog, but it also hangs:

&"$exe" -s --capture=no --log-cli-level=$loglevel \
--webhost=$testenv --durations=0 --tb=short .\tests \
--html=$testenv.html > $outputlog 2>&1

pytest : have its own capturing control the -s --capture=no which should mean no capturing, changing those variables did not help anything

After much trial and error I found piping to Out-File blocks closing the file until all processes started in the scriptblock has been terminated.

I use environment variables to control settings and one option I use during development is to keep the webdriver and webbrowser open, so I can jump in and fix in case of error.

If I close all processes started in scriptblock it completes. This is originally a port from linux where this is no problem whatsoever.

So my solution is to set the variables explicitly to a non blocking condition.

if ( -not (Test-Path "$($pwd)\win\")) {
   $ENV:PATH+=";" + $pwd + "\win\"
}
$ENV:WEBDRIVER='ChromeHeadless'
$ENV:SCREENSHOT=1
$ENV:WEBDRIVER_LANG='en'
if (Test-Path env:WEBDRIVER_OPEN) {
   # if set will block outfile due to open process in $cmd
   [Environment]::SetEnvironmentVariable('WEBDRIVER_OPEN', $null, 'User')
}
Invoke-Command -ScriptBlock {
   Invoke-Expression $cmd | Out-File -NoClobber -FilePath $outputlog
}

All this for the summary line of pytest, which is used to define status of tests:

Get-Content -Path .\output.log -tail 1 
======================== 3 passed in 62.18s (0:01:02) =========================

Thanks for all the valuable input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM