6

I'm attempting to use https://sshnet.codeplex.com/ to allow a PowerShell script to upload a file to a SFTP server. Everything appears to work, except it cannot find an overload of the method UploadFile and am stumped.

The definition of the method is here

TypeName   : Renci.SshNet.SftpClient
Name       : UploadFile
MemberType : Method
Definition : void UploadFile(System.IO.Stream input, string path, System.Action[uint64] uploadCallback),
             void UploadFile(System.IO.Stream input, string path, bool canOverride, System.Action[uint64] uploadCallback)

I'm trying to use this overload

UploadFile(System.IO.Stream input, string path, System.Action[uint64] uploadCallback)

The field uploadCallback is optional according to the documentation and isn't needed in my simple script, but even adding that in fails. The ways I've tried calling this with is as follows, they all fail.

How do I successfully call one of these methods? Examples of what I tried are below.

Examples

$client = New-Object Renci.SshNet.SftpClient($ftpHost, $ftpPort, $ftpUser, $ftpPass)
$client.Connect()

# ... get stream of file to upload here ...

$client.UploadFile($sourceStream, "$ftpPath$output")

Fails with

Cannot find an overload for "UploadFile" and the argument count: "2".
At F:\MyScript.ps1:170 char:2
+     $client.UploadFile($sourceStream, "$ftpPath$output")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

The next attempts all fail with the same error message essentially

$action = [System.Action[uint64]]
$client.UploadFile($sourceStream, "$ftpPath$output", $action)

Error

Cannot find an overload for "UploadFile" and the argument count: "3".
At F:\MyScript.ps1:170 char:2
+     $client.UploadFile($sourceStream, "$ftpPath$output", $action)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Attempted with a $null 3rd parameter

$client.UploadFile($sourceStream, "$ftpPath$output", $null)

Failed with

Cannot find an overload for "UploadFile" and the argument count: "3".
At F:\MyScript.ps1:169 char:2
+     $client.UploadFile($sourceStream, "$ftpPath$output", $null)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • 1
    Try `$client.UploadFile($sourceStream, "$ftpPath$output", [Action[uint64]]$null)`. Also, is $sourceStream a stream i.e. does `$sourceStream -is [System.IO.Stream]` return True? – Keith Hill Oct 16 '14 at 22:35
  • 1
    It might help to trim this post down. It's got a lot to go through to get to the meat of the question. Maybe even reorganize it so some more important details are at the beginning. – jpmc26 Oct 16 '14 at 23:38
  • @KeithHill That was the problem, I was passing a `StreamReader` instead of a `Stream`. If you fill out an answer I'll accept it, thank you. – Kirk Oct 17 '14 at 14:31
  • Cool. I created an answer from the comment. – Keith Hill Oct 17 '14 at 17:54

1 Answers1

5

Try giving PowerShell a bit more help by providing the type info as cast in the method call e.g.:

$client.UploadFile($sourceStream, "$ftpPath$output", [Action[uint64]]$null)
Keith Hill
  • 194,368
  • 42
  • 353
  • 369