The task is very simple but I can't implement it. Need to exclude the system folder from the data that is synchronized to the remote backup server
My script
param (
$localPath = "E:\path",
$remotePath = "/disk_files_backup/disk_E",
)
try {
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "*"
UserName = "*"
Password = "*"
GiveUpSecurityAndAcceptAnySshHostKey = $True
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
# Example file types to include
$transferOptions.FileMask = "| System Volume Information*"
# Synchronize files to remote directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote,
$localPath,
$remotePath,
$True,
$transferOptions
)
$synchronizationResult
}
finally {
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch {
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
tried different formats for specifying the folder, also exclude all folders using this method "| *" nothing works for me
You have two issues in your code.
Mask for a directory have to end with a slash:
https://winscp.net/eng/docs/file_mask#directory
So like this:
| System Volume Information*/
Or */
, when excluding all directories.
See How do I transfer (or synchronize) directory non-recursively?
TransferOptions options
is 7th argument of Session.SynchronizeDirectories
, not 5th:
https://winscp.net/eng/docs/library_session_synchronizedirectories
So the call should be like:
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote,
$localPath,
$remotePath,
$True,
$False, # Missing 'mirror' parameter
[WinSCP.SynchronizationCriteria]::Time, # Missing 'criteria' parameter
$transferOptions
)
See also Why are options provided to WinSCP .NET assembly methods in PowerShell being ignored?