Change remote path before Session.Open(SessionOptions)

Advertisement

UATO
Guest

Change remote path before Session.Open(SessionOptions)

Hi,

I'm not being able to change the remote path before opening the session in PowerShell using .NET lib.
The problem is that I don't have access to the root of the remote server and it returns an exception on Open function.
Using UI I can define it in advanced settings but I couldn't find how to do it using the .Net assembly lib.

Code example:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{}
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = $HostName
$sessionOptions.PortNumber = $PortNumber
$sessionOptions.UserName = $UserName
$sessionOptions.Password = $Password
$sessionOptions.SshPrivateKeyPath = $SshPrivateKeyPath
$sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint
$sessionOptions.AddRawSettings('ProxyMethod', $ProxyMethod)
$sessionOptions.AddRawSettings('ProxyHost', $ProxyHost)
$sessionOptions.AddRawSettings('ProxyPort', $ProxyPort)
 
$session = New-Object WinSCP.Session
 
try {
     # Connect
     $session.Open($sessionOptions) # ERROR WHEN DON'T HAVE ACCESS TO ROOT
     
     # Download files
     $transferOptions = New-Object WinSCP.TransferOptions
     $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
     $transferResult = $session.GetFiles($remote, $local, $False, $transferOptions)
} finally {
      # Disconnect, clean up
      $session.Dispose()
}
It works fine as long as there is not this specific situation.

Thanks

Reply with quote

Advertisement

UATO
Guest

Apparently it is possible to define it in the RawSettings of SessionOptions using the not yet documented property RemoteDirectory. Got here checking the .ini file and finding what property was saving this information.

Reply with quote

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

Re: Change remote path before Session.Open(SessionOptions)

WinSCP is not trying to access a root. It's trying to access the home directory of your remote account. If your account has an inaccessible root set as the home directory, then it should be fixed on the server.

Reply with quote

Advertisement

You can post new topics in this forum