Powershell 如何为测试连接设置变量以获取IPV4地址?
我为这个糟糕的标题道歉,我不知道该怎么说 我想设置一个脚本,当运行时,它会执行一个if-then命令,输出计算机名、连接是否失败或成功以及连接后的IP是什么 到目前为止,我得到的是:Powershell 如何为测试连接设置变量以获取IPV4地址?,powershell,Powershell,我为这个糟糕的标题道歉,我不知道该怎么说 我想设置一个脚本,当运行时,它会执行一个if-then命令,输出计算机名、连接是否失败或成功以及连接后的IP是什么 到目前为止,我得到的是: $computer1 = 'google.com' $computer2 = 'netflix.com' if (Test-Connection "$computer1" -count 1 -Quiet) {"$computer1 connected"} el
$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
当然你可以加上“安静”
但它只是说:
False
True
任何帮助都将是巨大的,因为我被难住了这里有一种优雅地处理失败的方法
$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
看看我的答案,这正是我需要的。非常感谢你。