PowerShell找不到重载

时间:2014-10-16 21:58:18

标签: .net powershell

我尝试使用https://sshnet.codeplex.com/允许PowerShell脚本将文件上传到SFTP服务器。一切似乎都有效,除了它找不到方法UploadFile的重载并且难以接受。

方法的定义在这里

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)

我试图使用此重载

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

字段uploadCallback根据文档是可选的,在我的简单脚本中不需要,但即使添加失败也是如此。我尝试过这种方式的方式如下,它们都失败了。

如何成功调用其中一种方法?我试过的例子如下。

实施例

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

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

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

失败
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

接下来的所有尝试都以相同的错误消息失败

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

错误

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

尝试使用$null第3个参数

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

失败
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

1 个答案:

答案 0 :(得分:5)

通过在方法调用中提供类型信息,例如:

,尝试为PowerShell提供更多帮助
$client.UploadFile($sourceStream, "$ftpPath$output", [Action[uint64]]$null)