Testing connectivity to FTP server with PowerShell Test-Connection

5,971

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.

Share:
5,971

Related videos on Youtube

yazan
Author by

yazan

Updated on September 18, 2022

Comments

  • yazan
    yazan over 1 year

    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

    • Mokubai
      Mokubai over 6 years
      How 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.
    • flolilo
      flolilo over 6 years
      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.
    • yazan
      yazan over 6 years
      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"}
    • Jonathan Scion
      Jonathan Scion over 6 years
      first check whether ftp port is open $ftpClient = New-Object System.Net.Sockets.TCPClient; $ftpClient.Connect('$server',21); $ftpClient.Connected
  • WeatherForecastingRat
    WeatherForecastingRat over 6 years
    Martin 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
  • yazan
    yazan over 6 years
    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)
  • Martin Prikryl
    Martin Prikryl over 6 years
    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.
  • yazan
    yazan over 6 years
    cuz i want upload file.zpl
  • Martin Prikryl
    Martin Prikryl over 6 years
    How does that answer my question?
  • yazan
    yazan over 6 years
    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
  • Martin Prikryl
    Martin Prikryl over 6 years
    We 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!
  • yazan
    yazan over 6 years
    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
  • WeatherForecastingRat
    WeatherForecastingRat over 6 years
    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).
  • Martin Prikryl
    Martin Prikryl over 6 years
    @WeatherForecastingRat It's 21, not 22. + I do not get the point anyway, but it's up to the OP.
  • WeatherForecastingRat
    WeatherForecastingRat over 6 years
    :) correct 22 is ssh.
  • WeatherForecastingRat
    WeatherForecastingRat over 6 years
    @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.
  • yazan
    yazan over 6 years
    @WeatherForecastingRat yah finally somebody see my point :)
  • yazan
    yazan over 6 years
    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;
  • flolilo
    flolilo over 6 years
    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
    flolilo over 6 years
    still, 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.
  • yazan
    yazan over 6 years
    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
  • WeatherForecastingRat
    WeatherForecastingRat over 6 years
    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.
  • yazan
    yazan over 6 years
    that is my final script, i edit it , and it's work for me now , thanks for all :)