【问题标题】:Send arbitrary FTP commands in PowerShell在 PowerShell 中发送任意 FTP 命令
【发布时间】:2015-09-24 16:39:06
【问题描述】:

我一直在使用 Michal Gajda 的 PSFTP 模块做很多事情

直到我想发送任意命令,例如:

quote SITE LRECL=132 RECFM=FB
or 
quote SYST

我发现使用FTPWebRequest无法做到这一点,只能通过第三方FTP实现。

我想问一下,与PowerShell兼容的最好的开源FTP实现是什么?

【问题讨论】:

  • 我建议让作者实现这个功能。这可能是让您的自定义命令通过 Powershell 工作的最佳选择。

标签: powershell ftp


【解决方案1】:

您可以使用Session.ExecuteCommand method 发送带有WinSCP .NET assembly used from PowerShell 的任意FTP 命令:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
    $sessionOptions.HostName = "example.com"
    $sessionOptions.UserName = "user"
    $sessionOptions.Password = "password"

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Execute command
        $session.ExecuteCommand("SITE LRECL=132 RECFM=FB").Check() 
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}

还有基于 WinSCP .NET 程序集的 PowerShell module 构建,您可以像这样使用:

$session = New-WinSCPSessionOptions -Protocol Ftp -Hostname example.com -Username user -Password mypassword | Open-WinSCPSession
Invoke-WinSCPCommand -WinSCPSession $session -Command "SITE LRECL=132 RECFM=FB"

(我是 WinSCP 的作者)

【讨论】:

  • 非常感谢您的回答,当我尝试使用您的解决方案时,我收到以下错误“无法加载文件或程序集(HRESULT 异常:0x80131515)”您能帮我解决这个问题吗
  • 我已经将 dll 标记为不受保护,我解决了问题,将 WinSCP.exe 与 dll 放在我的文件夹中。如果没有必要,感谢您纠正我。
  • 是的,您必须将WinSCP.exe 放在WinSCPnet.dll 旁边。见installation instructions。尽管WinSCP.exe 的缺失不可能是“无法加载文件或程序集”异常的原因。
猜你喜欢
  • 2023-03-20
  • 2012-05-12
  • 2018-01-30
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多