Topic "PowerShell Script Not Working"

Author Message
dwilson9@charter.net
[View user's profile]

Joined: 2015-06-25
Posts: 2
I am trying to use the below script to upload files with SFTP but it is not working. The script executes and returns to a prompt, but my file never uploads. Can someone please take a look and see if they see something I am missing? I can add the credentials manually to the GUI and everything works correctly.




param (
$localPath = "XXXXXXXXX",
$remotePath = "XXXXXXXXXXX",
$backupPath = "XXXXXXXXXXX"
)

try
{
# Load WinSCP .NET assembly
Add-Type -Path "c:\progra~2\winscp\WinSCPnet.dll"

# Load WinSCP .NET assembly
#[Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null

# Connection Settings
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "XXXXXXXXXXX"
$sessionOptions.UserName = "XXXXXXXXXXX"
$sessionOptions.Password = "XXXXXXXXXXX"
$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = "true"
#$sessionOptions.SshHostKeyFingerprint =

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

# Upload files, collect results
$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
}
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
Guest




Resolved.
Nilesh

Guest


Anonymous wrote:
Resolved.

What was the problem back there, and it's resolution because I'm following the same issue.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License