ipv4address를 가져 오기 위해 테스트 연결에 대한 변수를 설정하는 방법은 무엇입니까?

Nov 18 2020

나는 끔찍한 제목에 대해 사과한다. 나는 이것을 어떻게 말하는지 전혀 몰랐다.

실행할 때 컴퓨터 이름을 출력하는 if-then 명령, 연결 실패 또는 성공 여부 및 연결되면 IP가 무엇인지 스크립트를 설정하고 싶습니다.

지금까지 내가 가진 것은 :

    $computer1 = 'google.com' $computer2 = 'netflix.com'
if (Test-Connection "$computer1" -count 1 -Quiet) {"$computer1 connected"} else {"$computer1 failed"} if (Test-Connection "$computer2" -Count 1 -Quiet) {"$computer2 connected"} else {"$computer2 failed"}

따라서 다음과 같이 출력됩니다.

Google.com 연결됨

Netflix.com 실패

반환 할 때 다음과 같이 표시되도록 한 단계 더 나아가려면 어떻게해야합니까?

Google.com 연결 "IPv4address"

IPv4 주소와 연결 및 컴퓨터 이름이 어디에 표시됩니까?

원래 테스트 연결을 할 때와 거의 비슷합니다.

test-connection google.com

그러면 다음이 반환됩니다.

Source        Destination     IPV4Address

------        -----------     -----------    
DOMINATOR     google.com      172.217.9.206
DOMINATOR     google.com      172.217.9.206
DOMINATOR     google.com      172.217.9.206
DOMINATOR     google.com      172.217.9.206

내가 묻는 이유는 연결이 실패하면 짜증나 기 때문입니다.

PS C:\Users\Andrew> Test-Connection netflix.com, google.com
Test-Connection : Testing connection to computer 'netflix.com' failed: Error due to lack of resources
At line:1 char:1
+ Test-Connection netflix.com, google.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (netflix.com:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Test-Connection : Testing connection to computer 'netflix.com' failed: Error due to lack of resources
At line:1 char:1
+ Test-Connection netflix.com, google.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (netflix.com:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Test-Connection : Testing connection to computer 'netflix.com' failed: Error due to lack of resources
At line:1 char:1
+ Test-Connection netflix.com, google.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (netflix.com:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Test-Connection : Testing connection to computer 'netflix.com' failed: Error due to lack of resources
At line:1 char:1
+ Test-Connection netflix.com, google.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (netflix.com:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand


Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
DOMINATOR     google.com      172.217.13.78                                             32       14
DOMINATOR     google.com      172.217.13.78                                             32       14
DOMINATOR     google.com      172.217.13.78                                             32       15
DOMINATOR     google.com      172.217.13.78                                             32       17

"-quiet"를 추가 할 수 있습니다.

그러나 다음과 같이 말합니다.

False
True

어떤 도움이라도 큰 도움이 될 것입니다.

답변

1 DougMaurer Nov 18 2020 at 11:23

다음은 오류를 정상적으로 처리하는 한 가지 방법입니다.

$targets = 'server1', 'server2', 'server3', 'dontexist' $success = Test-Connection -ComputerName $targets -Count 1 -ErrorAction SilentlyContinue -ErrorVariable errors | Select-Object @{n='Server';e={$_.address}},IPv4Address,@{n='Result';e={'Successful'}}

$failed = $errors.exception.message |
              Where-Object {$_ -match "computer '(.+?)'"} | Select-Object @{n='Server';e={$matches.1}},
                                @{n='IPv4Address';e={"N/A"}},
                                @{n='Result';e={'Failed'}}

$success + $failed