Reputation: 33
I apologize for the terrible title, i had no clue how to word this.
I want to setup a script that when ran, it does a if-then command that outputs the computer name, if it failed or succeeded to connect, and what the IP is if connected.
So far what I have is:
$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"}
So that outputs:
Google.com connected
Netflix.com failed
How can I take it a step further so that when it returns it says something like:
Google.com connected "IPv4address"
where it shows the IPv4 address as well as connected and the computername?
almost like how when you originally do test-connection:
test-connection google.com
and that returns:
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
Reason im asking is because when a connection fails, it throws an annoying:
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
and sure you can add "-quiet"
but then it just says:
False
True
Any help would be great cause im stumped
Upvotes: 1
Views: 928
Reputation: 8868
Here's one way to handle failures gracefully.
$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
Upvotes: 1