From 233a71ca433d83c5a5901a39fcb34254cd893300 Mon Sep 17 00:00:00 2001 From: Julian Noble Date: Sun, 24 Aug 2025 17:32:32 +1000 Subject: [PATCH] Fix getzig test-connection for powershell desktop --- bin/getzig.cmd | 45 +++++++++++++++++++++++++++++---------- src/scriptapps/getzig.ps1 | 45 +++++++++++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/bin/getzig.cmd b/bin/getzig.cmd index 3e1a859a..089b1dfd 100644 --- a/bin/getzig.cmd +++ b/bin/getzig.cmd @@ -931,27 +931,50 @@ if ($mirrors_response.StatusCode -eq 200) { $hostname = $uri.Host $dict_mirrors[$hostname] = @{} $dict_mirrors[$hostname]["uri"] = $mirror + $dict_mirrors[$hostname]["latency"] = 888888 ;#default $host_list += $hostname #write-host "Host name: $hostname" } #write-host "dict: $($dict_mirrors | out-String)" write-host "host_list: $host_list" - - $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet - for ($i = 0; $i -lt $test_results.Count; $i++) { - $result = $test_results[$i] - if ($result) { - $targethost = $result.Target - if ($result.Status -eq "Success") { - $dict_mirrors[$targethost]["latency"] = $result.Latency + if ($PSVersionTable.PSEdition -eq "Desktop") { + #powershell.exe + #we seem to need -ErrorAction Ignore for bad/unresponsive host entries - but unlike with pwsh, we don't get any corresponding entry in the test_results, and -Quiet doesn't seem to give us results either REVIEW + $test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore + for ($i = 0; $i -lt $test_results.Count; $i++) { + $result = $test_results[$i] + if ($result) { + $targethost = $result.Address + if ($result.StatusCode -eq 0) { + $dict_mirrors[$targethost]["latency"] = $result.ResponseTime + } else { + $dict_mirrors[$targethost]["latency"] = 999999 + } } else { + $targethost = $host_list[$i] $dict_mirrors[$targethost]["latency"] = 999999 } - } else { - $targethost = $host_list[$i] - $dict_mirrors[$targethost]["latency"] = 999999 } + } else { + #pwsh.exe + $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet + for ($i = 0; $i -lt $test_results.Count; $i++) { + $result = $test_results[$i] + if ($result) { + $targethost = $result.Target + if ($result.Status -eq "Success") { + $dict_mirrors[$targethost]["latency"] = $result.Latency + } else { + $dict_mirrors[$targethost]["latency"] = 999999 + } + } else { + $targethost = $host_list[$i] + $dict_mirrors[$targethost]["latency"] = 999999 + } + } + } + $list_mirror_dicts = @() #write-host "dict tested: $($dict_mirrors | Out-String)" foreach ($key in $dict_mirrors.Keys) { diff --git a/src/scriptapps/getzig.ps1 b/src/scriptapps/getzig.ps1 index e53450ba..53eae1ad 100644 --- a/src/scriptapps/getzig.ps1 +++ b/src/scriptapps/getzig.ps1 @@ -97,27 +97,50 @@ if ($mirrors_response.StatusCode -eq 200) { $hostname = $uri.Host $dict_mirrors[$hostname] = @{} $dict_mirrors[$hostname]["uri"] = $mirror + $dict_mirrors[$hostname]["latency"] = 888888 ;#default $host_list += $hostname #write-host "Host name: $hostname" } #write-host "dict: $($dict_mirrors | out-String)" write-host "host_list: $host_list" - - $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet - for ($i = 0; $i -lt $test_results.Count; $i++) { - $result = $test_results[$i] - if ($result) { - $targethost = $result.Target - if ($result.Status -eq "Success") { - $dict_mirrors[$targethost]["latency"] = $result.Latency + if ($PSVersionTable.PSEdition -eq "Desktop") { + #powershell.exe + #we seem to need -ErrorAction Ignore for bad/unresponsive host entries - but unlike with pwsh, we don't get any corresponding entry in the test_results, and -Quiet doesn't seem to give us results either REVIEW + $test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore + for ($i = 0; $i -lt $test_results.Count; $i++) { + $result = $test_results[$i] + if ($result) { + $targethost = $result.Address + if ($result.StatusCode -eq 0) { + $dict_mirrors[$targethost]["latency"] = $result.ResponseTime + } else { + $dict_mirrors[$targethost]["latency"] = 999999 + } } else { + $targethost = $host_list[$i] $dict_mirrors[$targethost]["latency"] = 999999 } - } else { - $targethost = $host_list[$i] - $dict_mirrors[$targethost]["latency"] = 999999 } + } else { + #pwsh.exe + $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet + for ($i = 0; $i -lt $test_results.Count; $i++) { + $result = $test_results[$i] + if ($result) { + $targethost = $result.Target + if ($result.Status -eq "Success") { + $dict_mirrors[$targethost]["latency"] = $result.Latency + } else { + $dict_mirrors[$targethost]["latency"] = 999999 + } + } else { + $targethost = $host_list[$i] + $dict_mirrors[$targethost]["latency"] = 999999 + } + } + } + $list_mirror_dicts = @() #write-host "dict tested: $($dict_mirrors | Out-String)" foreach ($key in $dict_mirrors.Keys) {