這是我關于 SO 的第一個問題,所以就這樣吧!
我開始使用一個簡單的 powershell 腳本,我打算用它來對一些歸檔軟體進行基準測驗。我想運行歸檔程式(在本例中為 Windows 10 上的 7-zip)并將控制臺輸出寫入文本檔案以保留為日志。然后,我想將開始時間、結束時間、檔案大小......等附加到同一個日志中。
問題是我很難將輸出發送到文本檔案。在多次嘗試中,我永遠無法讓它作業。我嘗試了 Out-File 以及正常的“>”重定向,但它仍然以空結尾。我什至嘗試在 Start-Process 上設定 -PassThru 引數,但它只發送物件屬性而不是主機內容。
奇怪的是,當我使用“>”重定向在 CMD 中運行此命令時,它按預期作業,并且我找到了一個包含預期內容的文本檔案。
這是我當前的 powershell 腳本:
$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"
uj5u.com熱心網友回復:
我正在回答我自己的問題,因為Santiago Squarzon和mklement0已經在我的 OP 的評論中提出了解決方案。
圣地亞哥允許我產生結果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"
基本上,要使用Start-Process和生成帶有輸出的文本檔案,我需要在-RedirectStandardOutput引數中指定它(我沒有使用)。
但是,輸出不會顯示在主機中,因為它直接轉到指定的文本檔案。
為此,mklement0 的解釋和 wiki 非常有用。使用&呼叫要好得多:
& $7zFilePath `"$argument`" | Out-File -FilePath ".\$archiveName.txt"
有了這個,我在主機中得到了輸出,并且它也被復制到了文本檔案中。
非常感謝你們倆。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449439.html
