我有非常基本功能,因此将远程文件夹同步到本地。这适用于FileTransferred
处理程序,我想在混合中添加FileTransferProgress
,然后使用Write-Progress
。但是我无法理解,因为看来我在会话打开时无法添加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
的两行和上面的函数尽可能多地工作(有一些逻辑缺陷,但它们存在于此问题之外,例如我需要更新{{{ 1}})。
为什么我不能在会话打开时添加$fileProgressEvent
处理程序?
答案 0 :(得分:2)