본문 바로가기
파워쉘(Powershell)/예제

파워쉘 Ping ( Test-Connection )

by 예배파 2024. 4. 5.

ping을 사용해서 대상 컴퓨터의 작동여부를 알 수 있고, 결과는 문자열을 반환합니다

 

 

 

Test-Connection에 -Count 1 -Quiet를 사용하면 결과를 True, False를 반환하기 때문에
스크립트내에서 사용하는데 편리합니다

 

Ping 성공

 

Ping 실패

 

 

사용예

$computers = '127.0.0.1','128.1.2.3','127.0.0.2'
ForEach($com in $computers){
    Write-Host $com
    if(Test-Connection -ComputerName $com -Count 1 -Quiet){
        Write-Host "Ping 성공"
    }else{
        Write-Host "Ping 실패" -BackgroundColor Red
    }
    Write-Host
}

실행결과