Powershell script to upload files and exclude some
Hi.
Im trying to upload all files exepc some files based on its name.
Ive made this script, and it does quite well, except the exclusion...
try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null
# Setup session options
$SessionOptions.DisableVersionCheck
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = "localhost"
$sessionOptions.UserName = "ftp1"
$sessionOptions.Password = "ftp1"
# $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "\\10.0.0.105\asdf\*"
$remotePath = "/asdf/efh/"
$backupPath = "\\10.0.0.105\asdf\ok\"
# Upload files, collect results
#$TransferOptions()
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "|e2b_0170*.xml","|e2b_0280*.xml","|e2b_0515*.xml"
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
Can anyone help me ?
Im trying to upload all files exepc some files based on its name.
Ive made this script, and it does quite well, except the exclusion...
try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null
# Setup session options
$SessionOptions.DisableVersionCheck
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = "localhost"
$sessionOptions.UserName = "ftp1"
$sessionOptions.Password = "ftp1"
# $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "\\10.0.0.105\asdf\*"
$remotePath = "/asdf/efh/"
$backupPath = "\\10.0.0.105\asdf\ok\"
# Upload files, collect results
#$TransferOptions()
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "|e2b_0170*.xml","|e2b_0280*.xml","|e2b_0515*.xml"
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
Can anyone help me ?