PowerShell Test-Connection: 현대적인 핑 테스트

오늘의 cmdlet은 PowerShell Test-Connection입니다. Test-Connection은 당연히 네트워크 연결을 테스트하는 cmdlet입니다. Test-Connection을 PowerShell의 인기 있는 ping 유틸리티의 구현으로 생각하십시오. 둘 다 ICMP를 공통으로 가지고 있지만 두 가지 방법이 내부에서 약간 다르다는 것을 알 수 있습니다.

이 cmdlet을 사용하는 것은 간단합니다. 가장 기본적인 것으로 ComputerName 매개 변수를 지정하면 목적지 호스트로 4개의 ICMP 요청이 전송됩니다.

PS> Test-Connection -ComputerName google.com
Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       47
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       90
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       88
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       205

이 출력은 ping.exe와 유사해 보이며 표면적으로도 그렇지만 Powershell test-connection은 ICMP 요청을 약간 다르게 발행합니다. ping.exe와 달리 Test-Connection은 로컬 컴퓨터의 WMI 클래스 Win32_PingStatus를 사용하여 ICMP 요청을 보냅니다. 로컬 WMI 리포지토리를 사용하면 로컬 WMI 리포지토리가 정상이어야만 Test-Connection이 작동합니다.

PowerShell Test-Connection의 객체 출력

또한 PowerShell의 아름다움과 같이 이 cmdlet은 콘솔에 바로 나타나는 것만 반환하지 않습니다. 우리는 더 많은 정보를 수집할 수 있는 풍부한 객체를 볼 수 있습니다.

출력을 변수에 할당하고 속성을 확인하면 더 많은 유용한 정보를 수집한 것을 볼 수 있습니다.

PS> $pingResults | Get-Member
TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus
Name                           MemberType     Definition
----                           ----------     ----------
PSComputerName                 AliasProperty  PSComputerName = __SERVER
Address                        Property       string Address {get;set;}
BufferSize                     Property       uint32 BufferSize {get;set;}
NoFragmentation                Property       bool NoFragmentation {get;set;}
PrimaryAddressResolutionStatus Property       uint32
PrimaryAddressResolutionStatus {get;set;}
ProtocolAddress                Property       string ProtocolAddress {get;set;}
ProtocolAddressResolved        Property       string
ProtocolAddressResolved {get;set;}
RecordRoute                    Property       uint32 RecordRoute {get;set;}
ReplyInconsistency             Property       bool ReplyInconsistency {get;set;}
ReplySize                      Property       uint32 ReplySize {get;set;}
ResolveAddressNames            Property       bool ResolveAddressNames {get;set;}
ResponseTime                   Property       uint32 ResponseTime {get;set;}
ResponseTimeToLive             Property       uint32 ResponseTimeToLive {get;set;}
RouteRecord                    Property       string[] RouteRecord {get;set;}
RouteRecordResolved            Property       string[] RouteRecordResolved {get;set;}
SourceRoute                    Property       string SourceRoute {get;set;}
SourceRouteType                Property       uint32 SourceRouteType {get;set;}
StatusCode                     Property       uint32 StatusCode {get;set;}
Timeout                        Property       uint32 Timeout {get;set;}
TimeStampRecord                Property       uint32[] TimeStampRecord {get;set;}
TimeStampRecordAddress         Property       string[] TimeStampRecordAddress {get;set;} TimeStampRecordAddressResolved Property       string[]
TimeStampRecordAddressResolved {get;set;}
TimestampRoute                 Property       uint32 TimestampRoute {get;set;}
<SNIP>

만약 내부 호스트를 테스트하는 경우, Test-Connection은 원격 호스트에 인증하기 위해 DCOM을 사용합니다. 기본적으로 패킷 레벨 DCOM 인증을 사용하지만 언제든 DcomAuthentication 매개변수로 인증 유형을 변경할 수 있습니다.

백그라운드 작업 사용

이 cmdlet은 백그라운드 작업으로 실행될 수도 있습니다. 대기하다가 결국 시간 초과될 호스트 대신 많은 원격 컴퓨터를 핑하려면 단순히 백그라운드 작업으로 보내면 편리합니다.

Powershell test-connections에 대한 PowerShell 도움말에 따르면 로컬 및 원격 컴퓨터 모두에서 PowerShell 원격 작동을 활성화해야하지만 이는 사실이 아닙니다. 아래에서 볼 수 있듯이 나는 google.com을 테스트하고 있는데 명령어는 여전히 잘 작동합니다.

Test-Connection -AsJob -ComputerName google.com
Get-Job | Receive-Job

간단하게 유지하기

마지막으로 컴퓨터가 응답하는지 여부에 대한 이진식 응답만 원한다면 항상 Quiet 매개변수를 사용할 수 있습니다. 서버가 온라인인지 여부를 빠르게 확인하기 위해 항상 Quiet 및 1의 Count를 사용하여 Test-Connection에 단일 ICMP 요청을 보내도록합니다.

PS> Test-Connection -ComputerName google.com -Quiet -Count 1
True

그것이 오늘의 cmdlet입니다! Powershell test-connection으로 가능한 대부분을 다루었지만 항상 PowerShell 도움말 콘텐츠를 확인하거나 전체 내용을 확인하려면 Microsoft 문서로 이동하십시오.

Source:
https://adamtheautomator.com/powershell-test-connection/