Skip to content

Commit 13e43a3

Browse files
committed
➕ Get ping results as hash table
1 parent 439a99a commit 13e43a3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Get-PingResultsHashTable {
2+
[CmdletBinding()]
3+
param (
4+
# A server name or array of server names
5+
[Parameter(Mandatory)]
6+
[string[]]
7+
$ServerList
8+
)
9+
10+
# Build an ordered hash table that has the server name as the key and the IP address [and any errors] as the value.
11+
$Servers = [ordered]@{}
12+
$ServerList | ForEach-Object {
13+
$Results = Test-NetConnection $_ -InformationLevel Detailed
14+
if ($Results.PingSucceeded) {
15+
$Servers.Add( $Results.ComputerName, $($Results.ResolvedAddresses.Where(
16+
{ $_.AddressFamily -eq 'InterNetwork' }).IPAddressToString) )
17+
} else {
18+
$Servers.Add( $Results.ComputerName, "$($Results.ResolvedAddresses.Where(
19+
{$_.AddressFamily -eq 'InterNetwork'}).IPAddressToString): $($Error[0].Exception.Message)" )
20+
}
21+
}
22+
$Servers
23+
}

0 commit comments

Comments
 (0)