首页 > 解决方案 > 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

标签: powershellpowershell-4.0

解决方案


推荐阅读