Testing connectivity to FTP server with PowerShell Test-Connection

1

1

I try with this script, but it didn't work for me

$file = "test.txt"

$filePath = "C:\" + $file

$server = "ftp://server"

IF (Test-Connection -ComputerName $server -Quiet -Count 1 -ErrorAction SilentlyContinue)
{
$ftp = $server+$file

$webclient = New-Object System.Net.WebClient

$uri = New-Object System.Uri($ftp)

"Uploading $File..."

$webclient.UploadFile($uri, $filePath)
}
ELSE
{write-host "error"}

when I run the script , I have message "error" in the host it's mean there isn't contact with the server ,but when I ping the server is respond

yazan

Posted 2017-08-31T09:47:25.203

Reputation: 147

1How didn't it work? What did it do? Did it do anything? Did you get any error messages? What exactly happens? You can edit your question to add details. – Mokubai – 2017-08-31T09:54:29.247

1

Can Test-Connection even be used that way? The documentation states that it Sends ICMP echo request packets ("pings") to one or more computers. and that you have to Type the computer names or type IP addresses in IPv4 or IPv6 format. What I try to say: Test-Connection won't work with ftp://, but only with the IP/URL itself.

– flolilo – 2017-08-31T11:40:45.263

what I'm trying to say if ( the server ping) do { upload the file from the path c:\file.txt to the FTP server } else {write "error"} – yazan – 2017-08-31T11:55:17.163

1first check whether ftp port is open $ftpClient = New-Object System.Net.Sockets.TCPClient; $ftpClient.Connect('$server',21); $ftpClient.Connected – Antony – 2017-08-31T13:11:38.423

Answers

1

As @flolilolilo already commented, the Test-Connection accepts a host name, not URL, so you have to call it with server only, not ftp://server.

Once you fix that, you will face another problem, that your URI is wrong, as you are missing a slash between server and test.txt. The URI should be ftp://server/test.txt.


And anyway, I do not see the point of calling Test-Connection. Just try to upload the file straight away.

Martin Prikryl

Posted 2017-08-31T09:47:25.203

Reputation: 13 764

2Martin is right. Test-Connection does not accept URLs, but hostnames and ips (v4 and 6). He is also right about Test-Connection as icmp echo (ping) is blocked in many network so it might return false even if server is running. If you want to check for certain opened ports, you can use Test-NetConnection cmdlet, e.g. Test-NetConnection -ComputerName ftpserver -Port 22 -InformationLevel Quiet -WarningAction SilentlyContinue – WeatherForecastingRat – 2017-08-31T20:51:39.990

my point is : i have list of ftp servers and i want to upload the same file to all ftp servers , but before uploading i need to know witch server is connecte to the network so i want modifier the script to do (test-connection) if the server is alive >> send file , if dead >> write message(error) – yazan – 2017-09-01T07:51:54.193

But why? Why don't you just try to upload? And if the upload fails, write the message. No connectivity testing would guarantee you, that the upload succeeds anyway. – Martin Prikryl – 2017-09-01T07:55:21.757

cuz i want upload file.zpl – yazan – 2017-09-01T08:00:01.750

2How does that answer my question? – Martin Prikryl – 2017-09-01T08:01:06.043

cuz i want upload file.zpl

I want send ZPL commands to the a lot of zebra printer to modifier the community Name , so i want to know if the file send correctly or no i don't want to check each IP with SNMP to verify if the community Name was changed – yazan – 2017-09-01T08:11:15.030

1We understand that you want to upload a file. And once again: testing connectivity does not guarantee you that an upload will succeed. You have to try the actual upload and check results, to see if all went ok. So there's no point whatsoever to test connectivity before upload. Just upload and test results! – Martin Prikryl – 2017-09-01T08:12:48.577

it's really complicated i well explaine all the idai

  • I have 200 zebra printer i want to change all password and the community name for all printer in one script ,cuz the password by defult is 1234 and community name is PUBLIC so first i want to check if the printers is connected to the network then i well send the command
  • < – yazan – 2017-09-01T08:18:47.663

Fine by me - that way you can log which printer was not responsive. However, my point from earlier comment was, that not all network devices respond to echo request (usually blocked for sec reasons). It is usually not the case with printers. If you want to test if ftp server is up - test the port on which the service is running (usually 22). – WeatherForecastingRat – 2017-09-01T08:24:30.653

@WeatherForecastingRat It's 21, not 22. + I do not get the point anyway, but it's up to the OP. – Martin Prikryl – 2017-09-01T08:25:09.653

:) correct 22 is ssh. – WeatherForecastingRat – 2017-09-01T08:25:41.320

@MartinPrikryl i think there might be some benefits of doing that, e.g. you create bunch of objects in the script and some other memory intensive tasks (not just ftp upload) foreach server. You also predict that the fair share of your network devices might be out - it is better to test connectivity before then to cut down on used resources. – WeatherForecastingRat – 2017-09-01T08:30:04.347

@WeatherForecastingRat yah finally somebody see my point :) – yazan – 2017-09-01T08:31:55.663

1still, I'd additionally try-catch the actual upload, since there's no guarantee that it just succeeds because the printer is switched on and available. – flolilo – 2017-09-01T09:07:06.413

0

I use command get-content to get a list of IP address and ping it, if the IP a Live open FTP session and send the file to the printer

$printers = get-content "C:\......\servers.txt"
$info="C:\CommunityName.zpl" 
$ftp = "ftp://$ip/dir/CommunityName.zpl" 
$user = "" 
$pass = ""

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)


 foreach ($ip in $printers){
 IF (Test-Connection -ComputerName $ip -Quiet -Count 1 -ErrorAction SilentlyContinue){

    try { $uri = New-Object System.Uri($ftp)
          $webclient.UploadFile($uri, $info)
          Write-Host "UploadFile it's done $ip"  -backGround Green
        } 

    catch { Write-Host "An Error occured while uploading file to: $Uri" Throw

        }
}
 ELSE{ Write-Host "no conacting $ip"  -backGround Red}
}

yazan

Posted 2017-08-31T09:47:25.203

Reputation: 147

that is my orginal script i want to change it to send file first to change community name, after that i well send SNMP command to change all password by SNMPSET command so when i change community name i well edit it inside the command line ex private : snmpget -v1 -c private $ip enterprises.10642.20.10.10.5.11.4.0; – yazan – 2017-09-01T08:37:30.720

so this is working now? if not: please modify your question if you want to add details - it is very confusing when answers become questions. – flolilo – 2017-09-01T09:03:49.130

that script is working for my to ping a list of server and send SNMP command , but i want to edit it to add lines for send file zpl via FTP – yazan – 2017-09-01T09:38:53.003

use System.Net.Webclient or System.Net.FtpWebRequest like in your question - the issue was with url vs hostname (hostname goes to test-connection url to webclient). Additionaly, you can install PSFTP module and handle it using cmdlets. – WeatherForecastingRat – 2017-09-01T12:58:55.547

that is my final script, i edit it , and it's work for me now , thanks for all :) – yazan – 2017-09-05T13:03:49.220