Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: SessionOptions.Password is set, but SessionOptions.UserName is not

Are you sure this is a WinSCP question? I believe that the error indicates that your get-psw function does not work correctly in the ISE environment. What did you do to verify that the credentials are retrieved correctly?
Rick

SessionOptions.Password is set, but SessionOptions.UserName is not

Hi,

1 - I can execute this script locally with success
2 - I have granted permissions for the account running the job on the folder.
3 - the job execute without any error, however does not synchronize local folder.
4 - 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."

Can you please help?

$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
}