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
  • 188,800
  • 56
  • 490
  • 992
raz_user
  • 127
  • 1
  • 1
  • 9

1 Answers1

3

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
  • 188,800
  • 56
  • 490
  • 992