powershell - Start-Process - how to capture output to a file?
问题描述
I'm launching puttysftp to download files but the process get occasionally stuck so I'm also capturing the process ID and will kill it in 2 minutes if stuck so other processes can continue.
Issue is - there's no SFTP log of the transfer so I don't know what happenned. redirecting within -ArgumentList doesn't produce any results. Is there another way of achieving the same results e.g. kill the process within 2 minutes and have full log in a PowerShell? Many thanks!
$logfile = Get-Date -UFormat "%Y-%m-%d"
Start-Transcript -Append -Path C:\Scripts\FTPs-PS\FTP-PS_logs\FTP-$logfile.txt
$process = Start-Process "C:\Program Files (x86)\PuTTY\psftp.exe" -ArgumentList "username@localhost -pw StrongPassword -batch -b C:\Scripts\sft.txt" -NoNewWindow -PassThru -Verbose
do {
start-sleep 5
if (-Not (Get-Process -Id ($process.Id))) {break}
$date = $process.StartTime | Out-String
$total = New-TimeSpan -Start $date -End (Get-Date)
} while ($total.TotalSeconds -lt 120)
if ((Get-Process -Id ($process.Id))) {Stop-Process -id ($process.Id)}
Stop-Transcript
解决方案
推荐阅读
- arcore - 如何使用 SceneForm 从 ViewRenderable / Node 创建位图?
- html - 带有文本的 Jquery load() 方法
- javascript - 调用 API 后,我无法检测到 innerHTML 中的 api 数据
- scala - 依赖类型的类型类实例
- python - 使用嵌套字典中列出的条件创建和填充新列
- google-cloud-dataflow - Google 对数据流生成的数据流作业日志收费
- vb.net - 将Datagidview值传递给Vb.net MS Access中的Cystal Report Sub-Report
- c# - 如何在覆盖函数中调用异步 api
- pixi.js - 仅在交互时更新 pixi.js 中的帧
- jsf - 展开的 SelectOneMenu 在滚动 DataTable 时保持位置