Powershell -别名数组值?



是否可以在数组中使用一些短名称/别名?例如,我们必须测试到FDQN的连接,但我们希望将结果显示为短名称。

$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> "

别名:GG = google.comYT = youtube.com

使用散列表,Keys将是要ping的主机,Values将是每个主机的别名:

$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])

最新更新