PowerShell Synchronize mode 'Both' with Transfer Options

Advertisement

johnhamborg
Joined:
Posts:
4
Location:
Earth

PowerShell Synchronize mode 'Both' with Transfer Options

I am struggling to include $TransferOptions when using Synchronize mode 'Both'. I looked through loads of documentation but just couldn't figure out how to make it work in powershell. Thanks for the support!

Here is a snippet of modified code based on the included 'KeepLocalUpToDate' extension.

Write-Host "Connecting..."
$session.Open($sessionOptions)
 
#Setup Transfer Options
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$TransferOptions.FilePermissions =  New-Object WinSCP.FilePermissions
$TransferOptions.FilePermissions.Octal = 0777
 
while ($True)
{
    Write-Host "Synchronizing changes..."
    $result =
        $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Both, $localPath, $remotePath,[WinSCP.SynchronizationCriteria]:: $TransferOptions )

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,552
Location:
Prague, Czechia

Re: PowerShell Synchronize mode 'Both' with Transfer Options

$session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Both, $localPath, $remotePath,
    $False, $False, [WinSCP.SynchronizationCriteria]::Time, $TransferOptions)
See https://winscp.net/eng/docs/library_session_synchronizedirectories and
Why are options provided to WinSCP .NET assembly methods in PowerShell being ignored?

Reply with quote

mixolydian
Joined:
Posts:
2
Location:
USA

Re: PowerShell Synchronize mode 'Both' with Transfer Options

I have possible similar syntax problems related to FileMask, or the method is not implemented for [WinSCP.SynchronizationMode]::Both.

Code snippet:
# Connect
$session.Open($sessionOptions)
 
#Transfer options
$transferoptions = New-Object WinSCP.TransferOptions -Property @{
    FileMask = "| temporary.wav"
}
              
# Synchronize files
$synchronizationResult = $session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Both, "E:\data\em", "/usr/local/em", $false, $transferoptions)
But I get the error:
Error: Exception calling "SynchronizeDirectories" with "5" argument(s): "Cannot mirror files in synchronization mode Both"
Transfers work without the $transferoptions in the $session.SynchronizeDirectories() but I need to exclude the transfer of specific files/types from ever being transmitted.
I also tried using the syntax from the OP for the transfer options with the same resulting error.

Reply with quote

mixolydian
Joined:
Posts:
2
Location:
USA

Re: PowerShell Synchronize mode 'Both' with Transfer Options

I figured it out. I did not address the required passing of the parameters in the $session.SynchronizeDirectories() statement between the 4th parameter that I set as false and the transfer options, which would be the transferoptions, which would be the 7th parameter. I did not really understand what you meant with your response to the OP with you example. After time to review I based it on your example and wallah!!!
Fix:
# Synchronize files
$synchronizationResult = $session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Both, "E:\data\em", "/usr/local/em", $false, $false, [WinSCP.SynchronizationCriteria]::Time, $transferoptions)
I considered deleting the post but figured someone else may have the same confusion.
I really loved the .NET Assembly / COM Library!
https://winscp.net/eng/docs/library

I will be donating soon so you will continue your journey on supporting this awesome project!

Reply with quote

Advertisement

You can post new topics in this forum