我嘗試使用這個腳本,但它對我不起作用
$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"}
當我運行腳本時,主機中出現訊息“錯誤”,這意味著沒有與伺服器聯繫,但是當我 ping 伺服器時,伺服器有回應
答案1
正如@flolilolilo 已經評論的那樣,Test-Connection
接受主機名,而不是 URL,因此您必須server
僅使用而不是 來呼叫它ftp://server
。
一旦你解決了這個問題,你將面臨另一個問題,你的 URI 是錯誤的,因為你缺少server
和之間的斜線test.txt
。 URI 應該是ftp://server/test.txt
.
無論如何,我看不出打電話有什麼意義Test-Connection
。只需嘗試立即上傳文件即可。
答案2
我使用命令get-content
來取得 IP 位址清單並對其進行 ping 操作,如果該 IP 處於活動狀態,則開啟 FTP 會話並將檔案傳送至印表機
$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}
}