2

I've been doing plenty of stuff using PSFTP Module by Michal Gajda

Until I wanted to send arbitrary commands such as :

quote SITE LRECL=132 RECFM=FB
or 
quote SYST

I have found that this can't be achieved using FTPWebRequest, but only a third-party FTP implementation.

I want to ask what is the best open source FTP implementation compatible with PowerShell?

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
raz_user
  • 117
  • 1
  • 1
  • 9
  • I suggest ask author to implement this functionality. This will likely be your best bet on getting your custom commands to work via Powershell. – Vesper Jul 07 '15 at 09:17

1 Answers1

2

You can send an arbitrary FTP command with WinSCP .NET assembly used from PowerShell using the Session.ExecuteCommand method:

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
}

There's also the PowerShell module build on top of WinSCP .NET assembly that you can use like:

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

(I'm the author of WinSCP)

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • Thank you so much for your answer, When I try to use your solution I get the following error "Could not load file or assembly (Exception from HRESULT: 0x80131515)" Can you help me solve that please – raz_user Jul 07 '15 at 10:23
  • See http://winscp.net/eng/docs/message_net_operation_not_supported or http://stackoverflow.com/q/13799260/850848 – Martin Prikryl Jul 07 '15 at 11:00
  • I already marked the dll as unprotected, I solved the issue bu placing the WinSCP.exe with the dll in my folder. Thanks for correcting me if this is not necessary. – raz_user Jul 07 '15 at 11:32
  • Yes, you have to place the `WinSCP.exe` along the `WinSCPnet.dll`. See [installation instructions](http://winscp.net/eng/docs/library_install#installing). Though the absence of `WinSCP.exe` could not be the cause of the "Could not load file or assembly" exception. – Martin Prikryl Jul 07 '15 at 11:36