在PowerShell

时间:2015-07-07 09:11:22

标签: powershell ftp

我使用Michal Gajda的PSFTP模块做了很多事情

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

quote SITE LRECL=132 RECFM=FB
or 
quote SYST

我发现使用FTPWebRequest无法实现,但只能使用第三方FTP实现。

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

1 个答案:

答案 0 :(得分:2)

您可以使用WinSCP .NET assembly used from PowerShell发送Session.ExecuteCommand method的任意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的作者)