PowerShell 테스트 연결을 사용하여 FTP 서버에 대한 연결 테스트

PowerShell 테스트 연결을 사용하여 FTP 서버에 대한 연결 테스트

이 스크립트를 사용해 보았으나 작동하지 않았습니다.

$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-Connectionserver은 URL이 아닌 호스트 이름을 허용하므로 가 아닌로만 호출해야 합니다 ftp://server.

server이 문제를 해결하면 및 사이에 슬래시가 누락되어 URI가 잘못되었다는 또 다른 문제에 직면하게 됩니다 test.txt. URI는 이어야 합니다 ftp://server/test.txt.


그리고 어쨌든 전화할 이유가 없는 것 같아요 Test-Connection. 바로 파일을 업로드해 보세요.

답변2

get-contentIP가 Live FTP 세션을 열고 파일을 프린터로 보내는 경우 명령을 사용하여 IP 주소 목록을 가져와 핑합니다.

$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}
}

관련 정보