简体   繁体   中英

Powershell script using start-process and piping out-file to text file, not working, produces blank text file

This is my first question on SO so here goes!

I'm getting started with a simple powershell script that I intend to use to benchmark some archiving software. I want to run the archiver (7-zip on Windows 10 in this case) and write the console output to a text file to keep as a log. Then, I want to append to that same log the start time, end time, file sizes... etc.

The problem is I'm having difficulty sending the output to the text file. During multiple tries I never could make it work. I tried Out-File and also the normal ">" redirect but it still ends empty. I even tried setting -PassThru parameter on Start-Process but it only sent the object properties instead of the Host contents.

Curiously, when I make this command run in CMD with the ">" redirect, it works as expected and I find a text file with the expected contents.

This is my current powershell script:

$7zFilePath = "C:\Program Files\7-Zip\7z.exe"
$dateStart = Get-Date
$contentsToArchive = "D:\Temp -Local\Attack on Titan- Before the Fall-002.jpg"
$workingFolder = "D:\Temp"
$archiveName = "testing {0:yyyy-MM-dd hh.mm.ss.ffff}" -f ($dateStart)
$argument = "a -t7z -m0=LZMA2 -mmt=on -mx9 -md=64m -ms=16g -mfb=273 -mqs=on -mtc=on -mta=on -bb3 `"$workingFolder\$archiveName.7z`" `"$contentsToArchive`""
Set-Location $workingFolder
Start-Process -FilePath $7zFilePath -NoNewWindow -ArgumentList $argument | Out-File ".\$archiveName.txt"

I'm answering my own question because Santiago Squarzon and mklement0 already suggested the solution in the comments to my OP.

Santiago's allowed me to produce the result with Start-Process :

$7zFilePath = "C:\Program Files\7-Zip\7z.exe"
$contentsToArchive = "D:\Temp -Local\Attack on Titan- Before the Fall-002.jpg"
$workingFolder = "D:\Games\Emulation\ROMS\GoodGen V3.21"
$archiveName = "testing {0:yyyy-MM-dd HH.mm.ss.ffff}" -f ($dateStart)

$argument = "a -t7z -m0=LZMA2 -mmt=on -mx9 -md=64m -ms=16g -mfb=273 -mqs=on -mtc=on -mta=on -bb3 `"$workingFolder\$archiveName.7z`" `"$contentsToArchive`""
Set-Location $workingFolder
Start-Process $7zFilePath "$argument" -NoNewWindow -Wait -RedirectStandardOutput ".\$archiveName.txt"

Basically, to use Start-Process and produce a text file with the output I needed to specify it in the -RedirectStandardOutput parameter (which I wasn't using).

However, the output isn't displayed in the Host because it goes straight to the specified text file.

For this, mklement0's explanation and wiki were really useful. Using the & call was much better:

& $7zFilePath `"$argument`" | Out-File -FilePath ".\$archiveName.txt"

With this, I get the output in the Host, and also it's copied to the text file.

Many thanks to you both.

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