У меня есть очень основная функция, поэтому синхронизируйте удаленную папку с локальной. Это хорошо работает с обработчиком 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
, и вышеприведенная функция работает так, как я этого хочу (есть некоторые логические недостатки, но они существуют вне этой проблемы, например, мне нужно обновить блок скриптов для $fileProgressEvent
).
Почему я не могу добавить обработчик FileTransferProgress
, пока сессия открыта?