OSDcloud web connection test Uri as customizable parameter
Is your feature request related to a problem? Please describe. Google.com is used as test Uri for an internet connection on multiple places in the code. One of our customers doesn't allow unauthenticated connections to google.com from their network.
Describe the solution you'd like It would be great if we could override the Uri globally by defining an alternative as a parameter.
hmmm great idea. i think a customer of ours has a similar requirement.
Would it work like that for you: $Urls = @( 'http://www.msftconnecttest.com/connecttest.txt' )
foreach ($Uri in $Urls){
try {
if ($null = Invoke-WebRequest -Uri $Uri -Method Head -UseBasicParsing -ErrorAction Stop) {
Write-Host -ForegroundColor DarkCyan "PASS: $Uri"
}
else {
}
}
catch {
Write-Host -ForegroundColor Yellow "FAIL: $Uri"
}
}
Could you test please with your network guys?
Hi Akos, thank you for the prompt reply.
My customer just tested the code, and it works for them. They are currently overriding google.com with microsoft.com after booting PE which is a non-persistent change.
If you need this connection check into WinPE, change the code in your boot media (and don't update the module and the start of the process). For WinRE, this change could be a bit more tricky.
Agree. Don't use google.com - that URL is not used/needed anywhere in the project - instead use download.microsoft.com since OSDCloud probably would fail if the site is inaccessible. The following is used on our end as a StartNet script. It will replace Test-WebConnection.ps1 and modify OSD.WinRE.WiFi.ps1 on the fly as well as change the hosts-file in WinPE.
` <# Funksjon som endrer all trafikk gjort mot google.com til download.microsoft.com #>
function FileDecode { <# Denne funksjonen MIME-dekoder en tekst og returnerer det som en fil. #> param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string]$filepath, $b64text ) if ($b64text.GetType().BaseType -eq "System.Array") { [string]$b64text = $b64text -join '' } try { [System.IO.File]::WriteAllBytes($filepath, [byte[]]$([System.Convert]::FromBase64String($b64text))) } catch { Write-Error "$($_.ToString())" Return $false } return $filepath }
<# MIME-enkodet fil med en funksjon for å teste om vi er på nett #> $TestWebConnection = @( "77u/DQpmdW5jdGlvbiBUZXN0LVdlYkNvbm5lY3Rpb24NCnsNCiAgICBbQ21kbGV0QmluZGluZygpXQ0KICAgIHBhcmFtDQogICAgKA0KICAgICAgICBbUGFyYW1ldGVyKFZhbHVlRnJvbVBpcGVsaW5lKV0NCiAgICA" "gICAgIyBVcmkgdG8gdGVzdA0KICAgICAgICBbU3lzdGVtLlVyaV0NCiAgICAgICAgJFVyaSA9ICdnb29nbGUuY29tJw0KICAgICkNCg0KICAgIGlmICgoLW5vdCAkVXJpLkxlbmd0aCkgLW9yICgkVXJpIC1pbWF0Y2" "ggImdvb2dsZS5jb20iKSkgew0KICAgICAgICAkVXJpID0gJ2h0dHA6Ly9kb3dubG9hZC5taWNyb3NvZnQuY29tJw0KICAgIH0NCg0KICAgICRQYXJhbXMgPSBAew0KICAgICAgICBNZXRob2QgPSAnSGVhZCcNCiAgI" "CAgICAgVXJpID0gJFVyaQ0KICAgICAgICBVc2VCYXNpY1BhcnNpbmcgPSAkdHJ1ZQ0KICAgICAgICBIZWFkZXJzID0gQHsnQ2FjaGUtQ29udHJvbCc9J25vLWNhY2hlJ30NCiAgICB9DQoNCiAgICB0cnkgew0KICAg" "ICAgICBXcml0ZS1WZXJib3NlICJUZXN0LVdlYkNvbm5lY3Rpb24gT0s6ICRVcmkiDQogICAgICAgIEludm9rZS1XZWJSZXF1ZXN0IEBQYXJhbXMgfCBPdXQtTnVsbA0KICAgICAgICAkdHJ1ZQ0KICAgIH0NCiAgICB" "jYXRjaCB7DQogICAgICAgIFdyaXRlLVZlcmJvc2UgIlRlc3QtV2ViQ29ubmVjdGlvbiBGQUlMOiAkVXJpIg0KICAgICAgICAkZmFsc2UNCiAgICB9DQogICAgZmluYWxseSB7DQogICAgICAgICRFcnJvci5DbGVhci" "gpDQogICAgfQ0KfQ0K" )
$filepath = $(Join-Path -Path $pshome -ChildPath "Profile.ps1") try { [io.file]::OpenWrite($filepath).close() . $filepath } catch { }
$filepath = $null $gotError = $false
<# Finn nyeste versjon av OSD modulen #> [array]$versions = @() foreach ($p in $($env:PSModulePath -split ';')) { if (Test-Path -Path $(Join-Path -Path $p -ChildPath "OSD") -ErrorAction SilentlyContinue) { foreach ($ps in $(Get-ChildItem -Path $(Join-Path -Path $p -ChildPath "OSD") -ErrorAction Stop).FullName) { if (Test-Path -Path $(Join-Path -Path $ps -ChildPath "Public\Functions\Other\Test-WebConnection.ps1") -ErrorAction SilentlyContinue) { $versions += $ps } } } } $version = $versions | Sort-Object -Descending | Select-Object -First 1 $subv = ($version -split '.')[-1] $newversion = $version -replace "$($subv)$", "$([int]$subv + 1)" $oldvnumber = ($version -split '\')[-1] $vnumber = ($newversion -split '\')[-1] $filepath = Join-Path -Path $newversion -ChildPath "Public\Functions\Other\Test-WebConnection.ps1"
<# Endre funksjonen slik at en test mot google.com blir til download.microsoft.com Legge inn støtte for Qualcomm kort #> if ($filepath) { Copy-Item -Path $version -Destination $newversion -Recurse (Get-Content -Path "$newversion\OSD.psd1") -replace "$oldvnumber", "$vnumber" | Out-File -FilePath "$newversion\OSD.psd1" -Force $WinreWiFi = (Get-Content -Path "$newversion\Public\OSDCloudTS\OSD.WinRE.WiFi.ps1") -replace "google.com", "download.microsoft.com" $WinreWiFi = $WinreWiFi -replace '($WirelessNetworkAdapter = )(.)).}(.)', '$1$2 -or ($$_.Description -imatch "Wi-Fi")}$3' $WinreWiFi = $WinreWiFi -replace '(while (Get-CimInstance -ClassName Win32_NetworkAdapter)(.)).}).NetEnabled) -eq $false(.)', '$1$2 -or ($$_.Description -imatch "Wi-Fi")}).NetEnabled) -ne $$true$3' $WinreWiFi -replace 'Start-Sleep -Seconds 15', 'if ($(Test-WebConnection -Uri "https://download.microsoft.com")) {break}; Start-Sleep -Seconds 15' | Out-File -FilePath "$newversion\Public\OSDCloudTS\OSD.WinRE.WiFi.ps1" -Force #$WinreWiFi -replace 'Start-Sleep -Seconds 15', 'if ($(Test-WebConnection -Uri "https://download.microsoft.com")) {break}; Start-Sleep -Seconds 15' -replace '($WirelessNetworkAdapter = )(.)).}(.)', '$1$2 -or ($$.Description -imatch "Wi-Fi")$3' -replace '(while (Get-CimInstance -ClassName Win32_NetworkAdapter)(.)).}(.)', '$1$2 -or ($$.Description -imatch "Wi-Fi")$3' | Out-File -FilePath "$newversion\Public\OSDCloudTS\OSD.WinRE.WiFi.ps1" -Force (Get-Content -Path "$newversion\Public\OSDCloudTS\OSD.WinRE.WiFi.ps1") -replace "google.com", "download.microsoft.com" | Out-File -FilePath "$newversion\Public\OSDCloudTS\OSD.WinRE.WiFi.ps1" -Force $retval = FileDecode -filepath $filepath -b64text $($TestWebConnection -join '') if (-not $retval) { Write-Error "Klarte ikke redigere kommando." } else { Remove-Module -Name OSD -Force Import-Module -Name OSD -Force } } else { Write-Error "Fant ikke modul." }
<# Endre HOST-fila slik at google.com peker mot download.microsoft.com #> $dnsname = "download.microsoft.com" try { $ip = [System.Net.Dns]::GetHostAddresses($dnsname).IpAddressToString | Select-Object -First 1 $hostfile = "$($env:windir)\System32\drivers\etc\hosts" "# redirect google.com to $($dnsname)" | Out-File -FilePath $hostfile -Append -Encoding utf8 "$($ip) google.com" | Out-File -FilePath $hostfile -Append -Encoding utf8 } catch { $hostfile = "$($env:windir)\System32\drivers\etc\hosts" "# redirect google.com to $($dnsname)" | Out-File -FilePath $hostfile -Append -Encoding utf8 "184.50.200.164 google.com" | Out-File -FilePath $hostfile -Append -Encoding utf8 } `