
Intenté con este script, pero no funcionó para mí.
$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"}
Cuando ejecuto el script, aparece el mensaje "error" en el host, lo que significa que no hay contacto con el servidor, pero cuando hago ping, el servidor responde.
Respuesta1
Como ya comentó @flolilolilo, elTest-Connection
acepta un nombre de host, no una URL, por lo que debe llamarlo server
solo con , no con ftp://server
.
Una vez que solucione eso, enfrentará otro problema: su URI es incorrecto, ya que le falta una barra entre server
y test.txt
. La URI debería ser ftp://server/test.txt
.
Y de todos modos, no veo el sentido de llamar Test-Connection
. Intente cargar el archivo de inmediato.
Respuesta2
Utilizo el comando get-content
para obtener una lista de direcciones IP y hacer ping, si la IP es una sesión FTP abierta en vivo y envío el archivo a la impresora.
$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}
}