1

is it possible to use some short names / aliases for values in Array ? For example, we must test connection to FDQN but we would like to display result as short names.

$array = @( "youtube.com" , "google.com" )

$result = test-connection $array -count 1 -asjob | receive-job -wait
$result | select Address,ResponseTime | sort-object -Property ResponseTime

write-host "Fastest server is: <alias> "

Aliases: GG = google.com YT = youtube.com

  • 1
    You can make a hashtable with the real site urls as the key and an “alias” as the value - e.g. ```$lookups = @{ “youtube.com” = “YT”; “google.com” = “GG” }``` and then ```write-host “fastest site = $($lookups[@($result)[0].Address])”``` – mclayton Apr 07 '23 at 12:49

1 Answers1

0

Use a hash table, the Keys would be the hosts to ping and and the Values would be the alias for each host:

$hash = @{
    'youtube.com' = 'GG'
    'google.com'  = 'YY'
}

$result = Test-Connection @($hash.Keys) -Count 1 -AsJob |
    Receive-Job -Wait -AutoRemoveJob

$sorted = $result | Select-Object Address, ResponseTime |
    Sort-Object -Property ResponseTime

$sorted | Format-Table

Write-Host ('Fastest server is: {0}' -f $hash[$sorted[0].Address])
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Works perfectly, thank you !!! Is it possible also to display full result of test-connection in table view ? I tried Write-Host $sorted but it is listed in 1 row... – jarodas2004 Apr 07 '23 at 13:42
  • @jarodas2004 you mean like adding the alias? or just display the result before the "Fastest server..." message? If so, you can just output `$sorted` before the message (without `Write-Host`) – Santiago Squarzon Apr 07 '23 at 13:43
  • 1
    Unfortunately the result table is always displayed on the end, after the "Fastest server..." It looks like 1 command do not wait for previous to finish.. – jarodas2004 Apr 07 '23 at 14:19
  • @jarodas2004 use `Format-Table`, that will force it to be displayed first. `$sorted | Format-Table` – Santiago Squarzon Apr 07 '23 at 14:21