Hi Martin,
I am using the WinSCP PowerShell script to move the files in two different folders from local directory.
We are receiving PDF and XML files at the same time to our local machine. We need to send both XML and PDF in one remote directory and the same PDF only we need to move into another remote directory.
For that I wrote the below script. But anyhow we are receiving many files are missing in the folder which we are using for transfer only the PDF.
MY Script:
param (
$backupPath = "D:\Backup\occ_archive\"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "tardis-sftp.pinternal.net"
UserName = "muraai"
SshHostKeyFingerprint = "ssh-rsa 2048 bO+fomFyD2OdtOZ6AJPQu1JRWh3LZQTpv/WPEUinLP0="
SshPrivateKeyPath = "D:\Software\SSH-Key\occ_prod.ppk"
}
$session = New-Object WinSCP.Session
try
{
$Session.SessionLogPath = "D:\Log\log2.log"
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
PreserveTimestamp = $False
ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}
$transferResult =
$session.PutFiles("D:\OCC\XML_PROD\*.pdf", "/pinit-sftp-tardis/muraai/occ_prod/oracle_pdf/", $False, $transferOptions)
$transferResult1 =
$session.PutFiles("D:\OCC\XML_PROD\*\", "/pinit-sftp-tardis/muraai/occ_prod/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
$transferResult1.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
Copy-Item $transfer.FileName $backupPath
Write-Host "Copy of $($transfer.FileName) completed"
}
foreach ($transfer1 in $transferResult1.Transfers)
{
Write-Host "Upload of $($transfer1.FileName) succeeded"
Remove-Item $transfer1.FileName
Write-Host "Deletion of $($transfer1.FileName) completed"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}