Unable to connect to company FTP

I am still new at PowerShell, so be gentle :slight_smile:

I have for some time now been trying to connect to a FTP, but no matter how many different scripts i tried, none of them work, I think I might have tried +10 different scripts but i keep running into errors like:

Set-FTPConnection : Exception calling “GetResponse” with “0” argument(s): “Unable to connect to the remote server”

Set-FTPConnection : Exception calling “GetResponse” with “0” argument(s): “The remote certificate is invalid according to the validation procedure.”

New-SFTPSession : Connection failed to establish within 10000 milliseconds.

The most promising script i found is this one:

#This function configures the necessary details for the script to connect to the FTP server 
function ConnectFTP () 
{ 
	$User = "USERNAME" 
	$Pass = "PASSWORD" 
	$EncryptedPass = ConvertTo-SecureString -String $Pass -asPlainText -Force 
	$Credentials = New-Object System.Management.Automation.PSCredential($User,$EncryptedPass) 
	$Server = "HOSTNAME" #THE FTP DOES NOT USE DEFAULT PORT, UNSURE WHERE TO ADD PORT NO ?
	
	#Connect to FTP site 
	#Store the connection output into a variable called $ResponseStatus 
	$ResponseStatus = Set-FTPConnection -Credentials $Credentials -Server $Server -Session MySession -UsePassive 
	
	#Evaluate the contents of $ResponseStatus using ConnectionStatus function 
	if (ConnectionStatus $ResponseStatus -eq $True) 
	{ 
		#If the connection was successful, list the files in the given path 
		Get-FTPChildItem -Session MySession -Path ftp://HOSTNAME/FOLDER
	} 
	
	else 
	{ 
		#If the connection failed, write the below string to screen 
		Write-Host "Connection to FTP server failed!" 
	} 
} 

#This function will parse the content of a required parameter $ResponseStatus 
function ConnectionStatus ($ResponseStatus) 
{ 
	#If $ResponseStatus contains the string "*Successfully transferred*", then the connection is considered successful 
	if (($ResponseStatus | Select -ExpandProperty StatusDescription) -like "*Successfully transferred*") 
	{ 
		#Success 
		#By returning $True back to the function that called this function, we can step into the if/else condition in the ConnectFTP function 
		return $True 
	} 
	
	else 
	{ 
		#Fail return $False 
	} 
}
	 
#Program Starts Here 
ConnectFTP

This script results in:

Set-FTPConnection : Exception calling "GetResponse" with "0" argument(s): "Unable to connect to the remote server"
At D:\Routines\ftpv5.ps1:12 char:20
+ ... nseStatus = Set-FTPConnection -Credentials $Credentials -Server $Serv ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Set-FTPConnection

Daniel,
Welcome to the forum.

When you have problems with scripts or modules you have found in the internet for example in the PowerShellGallery you should contact the outhor first and try to ask there for help.

Hi Olaf,
Thank you.

Okay, that makes sense. The scripts articel is +2 years old, but maybe I will get lucky.

@DankyDuck82 Let’s take it a step at a time. If you open PS, type in ftp, then use the open command, does it work? Something like this:

image

image

Pretty sure (it’s a long time since I used it) the native ftp command does not support FTPS.

Your first post mentions New-SFTPSession and SFTP is not FTP. The key differences are:

  • FTP - No encryption
  • FTPS - FTP with TLS encryption
  • SFTP - FTP over SSH

I’m assuming your Set-FTPConnection is from the PSFTP module. If so, try the command using the -EnableSSL and -IgnoreCert parameters.

Have you checked you can connect using a FTP client such as WinSCP?