$localPath = "\\folder1\log"
$remotePath = "/folder1/LOG"
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$SId = 1
function get-psw{
param(
[String]$SID
)
[Hashtable]$return = @{}
#Get Username and Password
$SS = ''
$SSWS = New-WebServiceProxy -Uri $SS -UseDefaultCredential
$Secret = $SSWS.GetSecret($SID, $false, $null)
$psw = (($Secret.Secret.Items | Where-Object{ $_.FieldDisplayName -eq 'Password' }).Value)
$usr = (($Secret.Secret.Items | Where-Object{ $_.FieldDisplayName -eq 'Username' }).Value)
if (!$psw)
{
$msg = "Couldn't get credentials"
Write-Error -Message $msg -ErrorAction Stop
exit 1
}
return @{username=$username;password=$password}
}
try
{
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = ""
Username = $usr
Password = $psw
FtpSecure = [WinSCP.FtpSecure]::Explicit
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Synchronize files
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
# Throw on any error
$synchronizationResult.Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}