我有一个很的基本功能,所以同步一个远程文件夹到本地。这与FileTransferred
处理程序配合良好,我想将FileTransferProgress
添加到组合中,然后使用Write-Progress
。但是我无法理解,因为在会话打开时,我无法添加FileTransferProgress
处理程序。为什么在会话打开时不能添加FileTransferProgress处理程序?
function Sync-RemoteToLocalFolder{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[WinSCP.Session]$Session,
[Parameter(Position=0)]
[string]$RemotePath="/",
[Parameter(Position=1,mandatory)]
[string]$LocalPath
)
$fileTransferedEvent = {Invoke-FileTransferredEvent $_}
# Determine how many files are in this folder tree
$fileCount = (Get-WinSCPItems -Session $Session -RemotePath $RemotePath | Where-Object{$_.IsDirectory -eq $false}).Count
$fileProgressEvent = {Invoke-FileProgressEvent $_ $fileCount}
try{
# Set the file transfer event handler
Write-Verbose "Setting Transfered handler"
$session.add_FileTransferred($fileTransferedEvent)
# Set the transfer progress handler
Write-Verbose "Setting Progress handler"
$Session.add_FileTransferProgress($fileProgressEvent)
# Sync the directories
Write-Verbose "Syncronizing '$LocalPath' with '$RemotePath'"
$synchronizationResult = $session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local, $LocalPath, $RemotePath, $False)
# Check the result for errors
$synchronizationResult.Check()
# Remove the handlers from the session
$session.remove_FileTransferred($fileTransferedEvent)
$Session.remove_FileTransferProgress($fileProgressEvent)
}catch [Exception]{
Write-Error $_
}
}
如果我运行此,以开放的$session
过去了然后我得到的消息Sync-RemoteToLocalFolder : Session is already opened
。我发现奇怪,因为我添加了一种不同类型的处理程序,但它们的功能可能不同。所以我可以注释掉关于FileTransferProgress
的两行,上面的函数的工作原理和我想要的一样多(存在一些逻辑缺陷,但它们存在于此问题之外,例如我需要更新$fileProgressEvent
的脚本块)。
为什么在会话打开时不能添加FileTransferProgress
处理程序?
我想通了。我很欣赏信息和文档更新。我从来没有想过添加一个标志,以便可以工作 – Matt