如何从PowerShell中使用WinSCP .NET组件显示正在下载的文件。

huangapple go评论123阅读模式
英文:

How can I show files being downloaded in WinSCP .NET assembly from PowerShell

问题

以下是翻译好的部分:

"I have a script that downloads some recordings from an FTP server and the downloads are saved in a folder inside a local path on my computer."

我有一个脚本,从FTP服务器下载一些录音,然后将它们保存在我的计算机上本地路径内的文件夹中。

"I have been able to trigger a message when all the recordings have been downloaded successfully, but I wanted instead to display a message every time each of the FTP recordings are downloaded. How can I do that?"

我已经成功地触发了一个消息,当所有录音都成功下载时,但我想在每次下载每个FTP录音时显示一条消息。我该如何做?

"This is the script:"

这是脚本:

英文:

I have a script that downloads some recordings from an FTP server and the downloads are saved in a folder inside a local path on my computer.

I have been able to trigger a message when all the recordings have been downloaded successfully, but I wanted instead to display a message every time each of the FTP recordings are downloaded. How can I do that?

This is the script:

  1. Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
  2. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  3. Protocol = [WinSCP.Protocol]::Ftp
  4. HostName = "ip of ftp"
  5. PortNumber = port number
  6. UserName = "user"
  7. Password = "credentials"
  8. }
  9. $session = New-Object WinSCP.Session
  10. try
  11. {
  12. $session.Open($sessionOptions)
  13. $transferOptions = New-Object WinSCP.TransferOptions
  14. $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
  15. $transferResult =
  16. $session.GetFiles($remotePath, $localPath, $False, $transferOptions)
  17. $transferResult.Check()
  18. foreach ($transfer in $transferResult.Transfers)
  19. {
  20. Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
  21. }
  22. }
  23. finally
  24. {
  25. $session.Dispose()
  26. }

I have looked for ways to do it but I can't find the answer.

答案1

得分: 1

If you want to show just the names of the files as they finish transferring, use Session.FileTransferred event:

  1. function FileTransferred
  2. {
  3. param($e)
  4. if ($e.Error -eq $Null)
  5. {
  6. Write-Host "Download of $($e.FileName) succeeded"
  7. }
  8. else
  9. {
  10. Write-Host "Download of $($e.FileName) failed: $($e.Error)"
  11. }
  12. }
  13. $session.add_FileTransferred( { FileTransferred($_) } )
  14. $session.Open($sessionOptions)
  15. ...
  16. $session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check()

If you want to display progress of individual file transfers, you can use Session.FileTransferProgress.

英文:

If you want to show just the names of the files as they finish transferring, use Session.FileTransferred event:

  1. function FileTransferred
  2. {
  3. param($e)
  4. if ($e.Error -eq $Null)
  5. {
  6. Write-Host "Download of $($e.FileName) succeeded"
  7. }
  8. else
  9. {
  10. Write-Host "Download of $($e.FileName) failed: $($e.Error)"
  11. }
  12. }
  13. $session.add_FileTransferred( { FileTransferred($_) } )
  14. $session.Open($sessionOptions)
  15. ...
  16. $session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check()

If you want to display progress of individual file transfers, you can use Session.FileTransferProgress.

huangapple
  • 本文由 发表于 2023年3月7日 20:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定