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
  • 397
  • 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.

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
  • 397
  • 1
  • 5
  • 20