PowerShell Script Not Working

Advertisement

dwilson9@charter.net
Joined:
Posts:
2

PowerShell Script Not Working

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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: PowerShell Script Not Working

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.

Reply with quote

Nilesh
Guest

What is the problem & resolution?

Anonymous wrote:

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

Reply with quote

Advertisement

You can post new topics in this forum