SessionOptions.Password is set, but SessionOptions.UserName is not
Hi,
- I can execute this script locally with success
- I have granted permissions for the account running the job on the folder.
- the job execute without any error, however does not synchronize local folder.
- when I run the script on the server from ISE i got this error:Error: Exception calling "Open" with "1" argument(s): "SessionOptions.Password is set, but SessionOptions.UserName is not."
$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 }