I am still new at PowerShell, so be gentle
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