PowerShell options

Advertisement

Burtamus
Joined:
Posts:
5
Location:
IA

PowerShell options

I'm trying to write a PowerShell Script to upload a newly created file. When I attempt to do so, it says...
Error: Exception calling "Check" with "0" argument(s): "Cannot overwrite remote file '/<filename>.ldif'.$$

Press 'Delete' to delete the file and create new one instead of overwriting it.$$
No such file or directory.
Error code: 2
Error message from server (US-ASCII): /<filename>.ldif"
I've been told to do two things...
  • For the Enable transfer resume/transfer to temporary filename for option, choose Disable.
  • For Transfer, choose Background, and clear the Use multiple connections for single transfer check box.
Easy enough in the GUI (BTW, the GUI works fine, not the PowerShell tho). How do I set these options in PowerShell? Examples greatly appreciated.

Reply with quote

Advertisement

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

Re: PowerShell options

Who did you tell that? Why do you think these options are relevant?

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session 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

Burtamus
Joined:
Posts:
5
Location:
IA

Sorry, I did not tell anyone that. It was told to me that the SFTP host required it set that way. I'll say again that the GUI works perfectly fine for uploading, it is just the command line that is having this issue.

Is there no documentation on how to set those options via the command line? It took me a week, but I finally found a way to log to a file. I'll work at getting that uploaded. Documentation on the command line is sadly lacking.
Thanks, Burt

Reply with quote

Burtamus
Joined:
Posts:
5
Location:
IA

This is the script I'm using to do the upload...
try {
    # Load WinSCP .NET assembly
    Add-Type -Path ".\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "sftp.*****.com"
        UserName = "*****"
        SshHostKeyFingerprint = "ssh-rsa 2048 *****"
        SshPrivateKeyPath = "D:\Source\*****.ppk"
    }
 
    $session = New-Object WinSCP.Session
 
    try {
        # Connect
        $session.SessionLogPath = ".\$($Filename).log"
        $session.Open($sessionOptions) 
 
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::ASCII
        $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
 
        $transferResult = $session.PutFiles($FileName, "/", $False, $transferOptions).Check()
    }
    finally {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch {
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Reply with quote

Advertisement

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

For the Enable transfer resume/transfer to temporary filename for option, choose Disable.
Your code already does that.

For Transfer, choose Background, and clear the Use multiple connections for single transfer check box
This is irrelevant for the .NET assembly, as it never uses multiple connections on its own.

So your problem must be elsewhere. Please post session log file from WinSCP GUI showing a successful upload.

Reply with quote

Burtamus

RESOLVED!

The issue was the remote path. Instead of just using /, the server was looking for ftp://<user>@<server>. This can be found in the GUI under Session/Generate Session URL.

Reply with quote

Advertisement

You can post new topics in this forum