Browse Source

getzig.ps1 short-circuit for hosts close to gitea1.intx.com.au

master
Julian Noble 2 weeks ago
parent
commit
24ee059261
  1. 460
      bin/getzig.cmd
  2. 64
      src/scriptapps/getzig.ps1

460
bin/getzig.cmd

@ -832,232 +832,240 @@ if ($matches.count) {
# -- --- --- --- --- --- --- --- --- --- --- --- --- ---begin powershell Payload # -- --- --- --- --- --- --- --- --- --- --- --- --- ---begin powershell Payload
#<powershell-payload> #<powershell-payload>
#launch example #launch example
#powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'cmd.exe' -ArgumentList @('/c', 'getzig.cmd') -NoNewWindow -Wait" #powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'cmd.exe' -ArgumentList @('/c', 'getzig.cmd') -NoNewWindow -Wait"
#powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'getzig.cmd' -NoNewWindow -Wait" #powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'getzig.cmd' -NoNewWindow -Wait"
#Join-Path using verbose method to support powershell 5? #Join-Path using verbose method to support powershell 5?
#$outbase = Join-Path -Path $PSScriptRoot -ChildPath "../.." #$outbase = Join-Path -Path $PSScriptRoot -ChildPath "../.."
$outbase = $PSScriptRoot $outbase = $PSScriptRoot
$outbase = Resolve-Path -Path $outbase $outbase = Resolve-Path -Path $outbase
$toolsfolder = Join-Path -Path $outbase -ChildPath "tools" $toolsfolder = Join-Path -Path $outbase -ChildPath "tools"
if (-not(Test-Path -Path $toolsfolder -PathType Container)) { if (-not(Test-Path -Path $toolsfolder -PathType Container)) {
#create folder - (can include missing intermediaries) #create folder - (can include missing intermediaries)
New-Item -Path $toolsfolder -ItemType Directory New-Item -Path $toolsfolder -ItemType Directory
} }
$zigfolder = Join-Path $toolsfolder -ChildPath "zig" $zigfolder = Join-Path $toolsfolder -ChildPath "zig"
$zigexe = Join-Path $zigfolder "zig.exe" $zigexe = Join-Path $zigfolder "zig.exe"
$releasearchive = "zig-x86_64-windows-0.15.1.zip" ;#zip on windows, tarball on every other platform $releasearchive = "zig-x86_64-windows-0.15.1.zip" ;#zip on windows, tarball on every other platform
Write-Output "powershell version: $($PSVersionTable.PSVersion)" Write-Output "powershell version: $($PSVersionTable.PSVersion)"
if (Get-Command $zigexe -ErrorAction SilentlyContinue) { if (Get-Command $zigexe -ErrorAction SilentlyContinue) {
Write-Host "zig.exe is installed in tools/zig" Write-Host "zig.exe is installed in tools/zig"
$zigv = tools/zig/zig.exe version 2>&1 $zigv = tools/zig/zig.exe version 2>&1
$stdout = $zigv | Where-Object {$_ -is [string]} $stdout = $zigv | Where-Object {$_ -is [string]}
$stderr = $zigv | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]} $stderr = $zigv | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]}
if ($stderr) { if ($stderr) {
Write-Host "Unexpected output from tools/zig/zig.exe: $stderr" Write-Host "Unexpected output from tools/zig/zig.exe: $stderr"
Write-Host "Consider deleting tools/zig and re-downloading" Write-Host "Consider deleting tools/zig and re-downloading"
} else { } else {
Write-Host "tools/zig/zig.exe version is: $stdout" Write-Host "tools/zig/zig.exe version is: $stdout"
} }
exit exit
} }
if (Get-Command "minisign" -ErrorAction SilentlyContinue) { if (Get-Command "minisign" -ErrorAction SilentlyContinue) {
Write-Host "minisign is available" Write-Host "minisign is available"
} else { } else {
Write-Host "minisign is missing. Will attempt to install using winget." Write-Host "minisign is missing. Will attempt to install using winget."
#Find-Module/Install-Module: older mechanism, available in powershell #Find-Module/Install-Module: older mechanism, available in powershell
#Find-PSResource/Install-PSResource: only available in newer pwsh etc? #Find-PSResource/Install-PSResource: only available in newer pwsh etc?
$wgclient = Get-Module -ListAvailable -Name Microsoft.WinGet.Client $wgclient = Get-Module -ListAvailable -Name Microsoft.WinGet.Client
if (${wgclient}.Length -eq 0) { if (${wgclient}.Length -eq 0) {
Write-Host "Microsoft.WinGet.Client module not installed.. will try to install." Write-Host "Microsoft.WinGet.Client module not installed.. will try to install."
Install-PackageProvider -Name NuGet -Force Install-PackageProvider -Name NuGet -Force
$psgallery_existing_policy = (Get-PSRepository -Name PSGallery).InstallationPolicy $psgallery_existing_policy = (Get-PSRepository -Name PSGallery).InstallationPolicy
if ($psgallery_existing_policy -eq "Untrusted") { if ($psgallery_existing_policy -eq "Untrusted") {
#Applies to all versions of PowerShell for the user, and is persistent for current user. #Applies to all versions of PowerShell for the user, and is persistent for current user.
#This has risks in that a powershell session started after this call, and before we reset it, will treat PSGallery as trusted #This has risks in that a powershell session started after this call, and before we reset it, will treat PSGallery as trusted
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
} }
Install-Module -Scope CurrentUser -Name Microsoft.Winget.Client -Force -Repository PSGallery Install-Module -Scope CurrentUser -Name Microsoft.Winget.Client -Force -Repository PSGallery
Repair-WinGetPackageManager Repair-WinGetPackageManager
import-module -name Microsoft.Winget.client import-module -name Microsoft.Winget.client
if ($psgallery_existing_policy -eq "Untrusted") { if ($psgallery_existing_policy -eq "Untrusted") {
Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
} }
} else { } else {
Write-Host "Microsoft.WinGet.Client is available" Write-Host "Microsoft.WinGet.Client is available"
} }
$wingetversion = (Find-WinGetPackage jedisct1.minisign).Version $wingetversion = (Find-WinGetPackage jedisct1.minisign).Version
if ($wingetversion) { if ($wingetversion) {
Write-Host "Installing minisign version: ${wingetversion}" Write-Host "Installing minisign version: ${wingetversion}"
Install-WinGetPackage -Id "jedisct1.minisign" Install-WinGetPackage -Id "jedisct1.minisign"
} }
if (Get-Command "minisign" -ErrorAction SilentlyContinue) { if (Get-Command "minisign" -ErrorAction SilentlyContinue) {
Write-Host "minisign is now available" Write-Host "minisign is now available"
} else { } else {
Write-Host "minisign is still not available" Write-Host "minisign is still not available"
#if we automatically relaunch - we could get stuck in a loop - ask user. #if we automatically relaunch - we could get stuck in a loop - ask user.
$response = read-host -Prompt "Relaunching process may make minizip available. Relaunch? Y|N" $response = read-host -Prompt "Relaunching process may make minizip available. Relaunch? Y|N"
#if ($PSVersionTable.PSEdition -eq "Desktop") { #if ($PSVersionTable.PSEdition -eq "Desktop") {
#powershell.exe #powershell.exe
#} else { #} else {
#pwsh.exe #pwsh.exe
#} #}
if ($response -ieq "y") { if ($response -ieq "y") {
Start-Process 'getzig.cmd' -NoNewWindow -Wait Start-Process 'getzig.cmd' -NoNewWindow -Wait
} }
exit exit
} }
} }
$zigpubkey = "RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U" $zigpubkey = "RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"
$mirrors_url = "https://ziglang.org/download/community-mirrors.txt" $mirrors_url = "https://ziglang.org/download/community-mirrors.txt"
$mirrors_response = $(Invoke-WebRequest -Uri $mirrors_url) $mirrors_response = $(Invoke-WebRequest -Uri $mirrors_url)
if ($mirrors_response.StatusCode -eq 200) { if ($mirrors_response.StatusCode -eq 200) {
#https://www.gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools/zig-x86_64-windows-0.15.1.zip #https://www.gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools/zig-x86_64-windows-0.15.1.zip
$mirror_array = @("https://gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools") $mirror_array = @("https://gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools")
$mirror_array += $mirrors_response.Content.TrimEnd("`r`n") -split "`r`n|`n" $mirror_array += $mirrors_response.Content.TrimEnd("`r`n") -split "`r`n|`n"
#$mirror_array += "https://bogusxxx.org" #test behaviour of a bogus/down entry #$mirror_array += "https://bogusxxx.org" #test behaviour of a bogus/down entry
$mirror_array += "https://ziglang.org" #main site $mirror_array += "https://ziglang.org" #main site
$dict_mirrors = [ordered]@{} $dict_mirrors = [ordered]@{}
$host_list = @() #same ordering as dict_mirrors $host_list = @() #same ordering as dict_mirrors
foreach ($mirror in $mirror_array) { foreach ($mirror in $mirror_array) {
$uri = New-Object System.Uri($mirror) $uri = New-Object System.Uri($mirror)
$hostname = $uri.Host $hostname = $uri.Host
$dict_mirrors[$hostname] = @{} $dict_mirrors[$hostname] = @{}
$dict_mirrors[$hostname]["uri"] = $mirror $dict_mirrors[$hostname]["uri"] = $mirror
$dict_mirrors[$hostname]["latency"] = 888888 ;#default $dict_mirrors[$hostname]["latency"] = 888888 ;#default
$host_list += $hostname $host_list += $hostname
#write-host "Host name: $hostname" #write-host "Host name: $hostname"
} }
#write-host "dict: $($dict_mirrors | out-String)" #write-host "dict: $($dict_mirrors | out-String)"
write-host "host_list: $host_list" write-host "host_list: $host_list"
if ($PSVersionTable.PSEdition -eq "Desktop") { $automation_name = "punkshell+julian@precisium.com.au_target_by_latency"
#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 $trace = test-netconnection gitea1.intx.com.au -Hops 6 -TraceRoute -ErrorAction Ignore
$test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore if ((${trace}.PingSucceeded) -and (${trace}.TraceRoute.Count -lt 5)) {
for ($i = 0; $i -lt $test_results.Count; $i++) { $dict_mirrors["gitea1.intx.com.au"]["latency"] = 2
$result = $test_results[$i] #short-circuit for hosts very near gitea1.intx.com.au
if ($result) { #don't even test others. This is primarily for hosts at intx.com.au and associated entities - which are more likely to use punkshell than anyone else.
$targethost = $result.Address } else {
if ($result.StatusCode -eq 0) { if ($PSVersionTable.PSEdition -eq "Desktop") {
$dict_mirrors[$targethost]["latency"] = $result.ResponseTime #powershell.exe
} else { #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
$dict_mirrors[$targethost]["latency"] = 999999 $test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore
} for ($i = 0; $i -lt $test_results.Count; $i++) {
} else { $result = $test_results[$i]
$targethost = $host_list[$i] if ($result) {
$dict_mirrors[$targethost]["latency"] = 999999 $targethost = $result.Address
} if ($result.StatusCode -eq 0) {
} $dict_mirrors[$targethost]["latency"] = $result.ResponseTime
} else { } else {
#pwsh.exe $dict_mirrors[$targethost]["latency"] = 999999
$test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet }
for ($i = 0; $i -lt $test_results.Count; $i++) { } else {
$result = $test_results[$i] $targethost = $host_list[$i]
if ($result) { $dict_mirrors[$targethost]["latency"] = 999999
$targethost = $result.Target }
if ($result.Status -eq "Success") { }
$dict_mirrors[$targethost]["latency"] = $result.Latency } else {
} else { #pwsh.exe
$dict_mirrors[$targethost]["latency"] = 999999 $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet
} for ($i = 0; $i -lt $test_results.Count; $i++) {
} else { $result = $test_results[$i]
$targethost = $host_list[$i] if ($result) {
$dict_mirrors[$targethost]["latency"] = 999999 $targethost = $result.Target
} if ($result.Status -eq "Success") {
} $dict_mirrors[$targethost]["latency"] = $result.Latency
} else {
} $dict_mirrors[$targethost]["latency"] = 999999
}
$list_mirror_dicts = @() } else {
#write-host "dict tested: $($dict_mirrors | Out-String)" $targethost = $host_list[$i]
foreach ($key in $dict_mirrors.Keys) { $dict_mirrors[$targethost]["latency"] = 999999
$list_mirror_dicts += $($dict_mirrors[$key]) }
} }
#need to ensure latency cast to integer (on powershell 5 at least)
$sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency } }
#Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)" }
Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)"
$automation_name = "punkshell+julian@precisium.com.au_target_by_latency" $list_mirror_dicts = @()
#write-host "dict tested: $($dict_mirrors | Out-String)"
foreach ($hostinfo in $sorted_mirror_dicts) { foreach ($key in $dict_mirrors.Keys) {
$uristring = $hostinfo.uri $list_mirror_dicts += $($dict_mirrors[$key])
if ($uristring -eq "https://ziglang.org") { }
$full_uristring = "${uristring}/download/0.15.1/${releasearchive}?source=${automation_name}" #need to ensure latency cast to integer (on powershell 5 at least)
$sig_uristring = "${uristring}/download/0.15.1/${releasearchive}.minisig?source=${automation_name}" $sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency }
} else { #Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)"
$full_uristring = "${uristring}/${releasearchive}?source=${automation_name}" Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)"
$sig_uristring = "${uristring}/${releasearchive}.minisig?source=${automation_name}"
} foreach ($hostinfo in $sorted_mirror_dicts) {
Write-Host "Downloading zig from $full_uristring" $uristring = $hostinfo.uri
#$uriobj = [uri]$full_uristring if ($uristring -eq "https://ziglang.org") {
#$lastSegment = $uriobj.Segments[-1] $full_uristring = "${uristring}/download/0.15.1/${releasearchive}?source=${automation_name}"
#$outfile = Join-Path $toolsfolder -ChildPath $lastSegment $sig_uristring = "${uristring}/download/0.15.1/${releasearchive}.minisig?source=${automation_name}"
$outfile = Join-Path $toolsfolder -ChildPath $releasearchive } else {
Write-Host "Download to: $outfile" $full_uristring = "${uristring}/${releasearchive}?source=${automation_name}"
Invoke-WebRequest -Uri $full_uristring -OutFile $outfile $sig_uristring = "${uristring}/${releasearchive}.minisig?source=${automation_name}"
}
if (-not(Test-Path -Path $outfile -PathType Leaf)) { Write-Host "Downloading zig from $full_uristring"
write-Host "Failed to download zig package from $full_uristring to ${outfile} .. aborting download from $uristring" #$uriobj = [uri]$full_uristring
continue #$lastSegment = $uriobj.Segments[-1]
} #$outfile = Join-Path $toolsfolder -ChildPath $lastSegment
Write-Host "Downloading minisig signature from $sig_uristring" $outfile = Join-Path $toolsfolder -ChildPath $releasearchive
Invoke-WebRequest -Uri $sig_uristring -Outfile "${outfile}.minisig" Write-Host "Download to: $outfile"
if (-not(Test-Path -Path "${outfile}.minisig" -PathType Leaf)) { Invoke-WebRequest -Uri $full_uristring -OutFile $outfile
write-Host "Failed to download minisig from $sig_uristring to ${outfile}.minisig .. aborting download from $uristring"
continue if (-not(Test-Path -Path $outfile -PathType Leaf)) {
} write-Host "Failed to download zig package from $full_uristring to ${outfile} .. aborting download from $uristring"
continue
write-host "downloaded $outfile and ${outfile}.minisig - validating signature..." }
#validate releasearchive (tarball/zip) Write-Host "Downloading minisig signature from $sig_uristring"
$sigresult = minisign -V -P $zigpubkey -m $outfile 2>&1 Invoke-WebRequest -Uri $sig_uristring -Outfile "${outfile}.minisig"
$stdout = $sigresult | Where-Object {$_ -is [string]} if (-not(Test-Path -Path "${outfile}.minisig" -PathType Leaf)) {
$stderr = $sigresult | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]} write-Host "Failed to download minisig from $sig_uristring to ${outfile}.minisig .. aborting download from $uristring"
$is_valid = $false continue
if ($stderr) { }
write-host "Signature validation failed with message: $stderr"
} else { write-host "downloaded $outfile and ${outfile}.minisig - validating signature..."
if (($stdout | Out-String).Contains("signature verified")) { #validate releasearchive (tarball/zip)
write-host $stdout $sigresult = minisign -V -P $zigpubkey -m $outfile 2>&1
$is_valid = $true $stdout = $sigresult | Where-Object {$_ -is [string]}
} else { $stderr = $sigresult | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]}
write-host "Unexpected output from minisign: $stdout" $is_valid = $false
write-host "Did not contain 'signature verified'" if ($stderr) {
} write-host "Signature validation failed with message: $stderr"
} } else {
if (-not($is_valid)) { if (($stdout | Out-String).Contains("signature verified")) {
write-Host "Couldn't verify signature from download site $uristring" write-host $stdout
Remove-Item -Path $outfile -ErrorAction SilentlyContinue $is_valid = $true
Remove-Item -Path "${outfile}.minisig" -ErrorAction SilentlyContinue } else {
continue write-host "Unexpected output from minisign: $stdout"
} write-host "Did not contain 'signature verified'"
}
Write-Host "Signature OK - extracting archive" }
Expand-Archive -path $outfile -DestinationPath $toolsfolder -Force if (-not($is_valid)) {
#Remove-Item -Path "$outfile" write-Host "Couldn't verify signature from download site $uristring"
$zip_rootname = [System.IO.Path]::GetFileNameWithoutExtension($releasearchive) Remove-Item -Path $outfile -ErrorAction SilentlyContinue
#Rename-Item -Path Remove-Item -Path "${outfile}.minisig" -ErrorAction SilentlyContinue
write-host "zip_rootname: $zip_rootname" continue
Rename-Item -Path $(Join-Path -Path $toolsfolder -ChildPath $zip_rootname) -NewName $zigfolder }
Write-Host "Zig installed in ${zigfolder}"
$zigexe = Join-Path $zigfolder -ChildPath "zig.exe" Write-Host "Signature OK - extracting archive"
$v = Invoke-Expression "$zigexe version" Expand-Archive -path $outfile -DestinationPath $toolsfolder -Force
Write-Host "ZIG VERSION: ${v}" #Remove-Item -Path "$outfile"
$test_ok = 1 ;#todo $zip_rootname = [System.IO.Path]::GetFileNameWithoutExtension($releasearchive)
if ($test_ok) { #Rename-Item -Path
break write-host "zip_rootname: $zip_rootname"
} Rename-Item -Path $(Join-Path -Path $toolsfolder -ChildPath $zip_rootname) -NewName $zigfolder
} Write-Host "Zig installed in ${zigfolder}"
$zigexe = Join-Path $zigfolder -ChildPath "zig.exe"
} else { $v = Invoke-Expression "$zigexe version"
Write-Host "Unable to retrieve list of zig community mirrors from $mirrors_url" Write-Host "ZIG VERSION: ${v}"
} $test_ok = 1 ;#todo
if ($test_ok) {
break
}
}
} else {
Write-Host "Unable to retrieve list of zig community mirrors from $mirrors_url"
}
#</powershell-payload> #</powershell-payload>

64
src/scriptapps/getzig.ps1

@ -103,42 +103,51 @@ if ($mirrors_response.StatusCode -eq 200) {
} }
#write-host "dict: $($dict_mirrors | out-String)" #write-host "dict: $($dict_mirrors | out-String)"
write-host "host_list: $host_list" write-host "host_list: $host_list"
if ($PSVersionTable.PSEdition -eq "Desktop") { $automation_name = "punkshell+julian@precisium.com.au_target_by_latency"
#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 $trace = test-netconnection gitea1.intx.com.au -Hops 6 -TraceRoute -ErrorAction Ignore
$test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore if ((${trace}.PingSucceeded) -and (${trace}.TraceRoute.Count -lt 5)) {
for ($i = 0; $i -lt $test_results.Count; $i++) { $dict_mirrors["gitea1.intx.com.au"]["latency"] = 2
$result = $test_results[$i] #short-circuit for hosts very near gitea1.intx.com.au
if ($result) { #don't even test others. This is primarily for hosts at intx.com.au and associated entities - which are more likely to use punkshell than anyone else.
$targethost = $result.Address } else {
if ($result.StatusCode -eq 0) { if ($PSVersionTable.PSEdition -eq "Desktop") {
$dict_mirrors[$targethost]["latency"] = $result.ResponseTime #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 { } else {
$targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} else {
$targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999
} }
} } else {
} else { #pwsh.exe
#pwsh.exe $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet
$test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet for ($i = 0; $i -lt $test_results.Count; $i++) {
for ($i = 0; $i -lt $test_results.Count; $i++) { $result = $test_results[$i]
$result = $test_results[$i] if ($result) {
if ($result) { $targethost = $result.Target
$targethost = $result.Target if ($result.Status -eq "Success") {
if ($result.Status -eq "Success") { $dict_mirrors[$targethost]["latency"] = $result.Latency
$dict_mirrors[$targethost]["latency"] = $result.Latency } else {
$dict_mirrors[$targethost]["latency"] = 999999
}
} else { } else {
$targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} else {
$targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999
} }
}
}
} }
$list_mirror_dicts = @() $list_mirror_dicts = @()
@ -150,7 +159,6 @@ if ($mirrors_response.StatusCode -eq 200) {
$sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency } $sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency }
#Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)" #Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)"
Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)" Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)"
$automation_name = "punkshell+julian@precisium.com.au_target_by_latency"
foreach ($hostinfo in $sorted_mirror_dicts) { foreach ($hostinfo in $sorted_mirror_dicts) {
$uristring = $hostinfo.uri $uristring = $hostinfo.uri

Loading…
Cancel
Save