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

Martin Prikryl
  • 21,071
  • 9
  • 77
  • 157
yazan
  • 367
  • 1
  • 5
  • 20

2 Answers2

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
  • 21,071
  • 9
  • 77
  • 157
  • 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 Sep 01 '17 at 07:55
  • 2
    How does that answer my question? – Martin Prikryl Sep 01 '17 at 08:01
  • 1
    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! – Martin Prikryl Sep 01 '17 at 08:12
  • @WeatherForecastingRat It's 21, not 22. + I do not get the point anyway, but it's up to the OP. – Martin Prikryl Sep 01 '17 at 08:25
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
  • 367
  • 1
  • 5
  • 20
  • 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 Sep 01 '17 at 08:37
  • 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 Sep 01 '17 at 09:03
  • 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 Sep 01 '17 at 09:38
  • 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 Sep 01 '17 at 12:58
  • that is my final script, i edit it , and it's work for me now , thanks for all :) – yazan Sep 05 '17 at 13:03