Bootstrap FreeKB - PowerShell - Connect to an FTPS Server
PowerShell - Connect to an FTPS Server

Updated:   |  PowerShell articles

Here is how you can test connection to an FTP server using PowerShell.

$server = "fpts.example.com"
$port = "21"

try {
  $client = New-Object System.Net.Sockets.TcpClient("$server", $port)
  $client.Close()
  Write-Output "Successfully Connected to $server on port $port"
}
catch {
  Write-Output "Connection Failed: $($_.Exception.Message)"
}
exit

 

If the connection fails, something like this should be returned.

Connection Failed: Exception calling ".ctor" with "2" argument(s): "No such host is known"

 

On the other hand, if the connection is successful, something like this should be returned.

Successfully Connected to server1.example.com on port 21

 

The Chilkat .net framework will be used, thus you'll need to download the appropriate .dll file from https://www.example-code.com/powershell/ftp_mutual_tls_authentication.asp and place the .dll file somewhere on your system. 

  • Set $ftp.Port to 21 or 990
  • Set $ftp.AuthTls to $true for an explicit TLS connection (typically port 21) or $false for an inplicit TLS connection (typically port 990)
  • Set $ftp.Ssl to $true for an explicit TLS connection or $false for an inplicit TLS connection

Then you could include the following to test making an FTPS connection to the FTPS server.

Add-Type -Path "C:\Users\john.doe\Downloads\chilkatdotnet48-9.5.0-x64\chilkatdotnet48-9.5.0-x64\ChilkatDotNet48.dll"

$ftp          = New-Object Chilkat.Ftp2
$ftp.Hostname = "ftps.example.com"
$ftp.Port     = 21
$ftp.AuthTls  = $true
$ftp.Ssl      = $false

$success = $ftp.ConnectOnly()
if ($success -eq $false) {
    $($ftp.LastErrorText)
    exit
}

$ftp.Username = "john.doe"
$ftp.Password = "itsasecret"

$success = $ftp.LoginAfterConnectOnly()
if ($success -eq $false) {
    $($ftp.LastErrorText)
    exit
}

Write-Output $($ftp)

$ftp.Disconnect()

 

If the FTPS server requires a SSL certificate to be presented when connecting (mutual authentication), include New-Object Chilkat.Cert, LoadPfxFile and SetSslClientCert.

Add-Type -Path "C:\Users\john.doe\Downloads\chilkatdotnet48-9.5.0-x64\chilkatdotnet48-9.5.0-x64\ChilkatDotNet48.dll"

$ftp          = New-Object Chilkat.Ftp2
$ftp.Hostname = "ftps.example.com"
$ftp.Port     = 21
$ftp.AuthTls  = $true
$ftp.Ssl      = $false

$cert = New-Object Chilkat.Cert

$success = $cert.LoadPfxFile("C:\Users\john.doe\Downloads\example.pfx","pfx password")
if ($success -eq $false) {
    $($cert.LastErrorText)
    exit
}

$success = $ftp.SetSslClientCert($cert)
if ($success -eq $false) {
    $($cert.LastErrorText)
    exit
}

$success = $ftp.ConnectOnly()
if ($success -eq $false) {
    $($ftp.LastErrorText)
    exit
}

$ftp.Username = "john.doe"
$ftp.Password = "itsasecret"

$success = $ftp.LoginAfterConnectOnly()
if ($success -eq $false) {
    $($ftp.LastErrorText)
    exit
}

Write-Output $($ftp)

$ftp.Disconnect()

 

If the connection is successful, something like this should be returned.

Greeting : 220 Service ready for new user.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter adc8e9 in the box below so that we can be sure you are a human.