Powershell wait for URL
function WaitForUrlContent { | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory=$true)] | |
[string]$ResponseContentNoPresent, | |
[Parameter(Mandatory=$false)] | |
[string]$Url="http://localhost:4502", | |
[Parameter(Mandatory=$false, | |
HelpMessage="Provide Basic Auth Credentials in following format: <user>:<pass>")] | |
[string]$BasicAuthCreds="", | |
[Parameter(Mandatory=$false)] | |
[string]$UserAgent="", | |
[Parameter(Mandatory=$false)] | |
[string]$Referer="", | |
[Parameter(Mandatory=$false)] | |
[string]$Timeout="5" | |
) | |
$HEADERS = @{ | |
} | |
if (-not([string]::IsNullOrEmpty($UserAgent))) { | |
$HEADERS.add("User-Agent",$UserAgent) | |
} | |
if (-not([string]::IsNullOrEmpty($Referer))) { | |
$HEADERS.add("Referer",$Referer) | |
} | |
if (-not([string]::IsNullOrEmpty($BasicAuthCreds))) { | |
$BASICAUTH = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($BasicAuthCreds)) | |
$HEADERS.add("Authorization","Basic $BASICAUTH") | |
} | |
Write-Host -NoNewline "Waiting for: $Url" | |
$FirstError=$true | |
$LastError="" | |
while ($true) { | |
try { | |
$response = Invoke-WebRequest -Headers $HEADERS -TimeoutSec $Timeout -Uri "$Url" | |
$StatusCode = $Response.StatusCode | |
} catch { | |
$StatusCode = $_.Exception.Response.StatusCode.value__ | |
$LastError = $StatusCode | |
if ($FirstError -Or $LastError -ne $StatusCode) { | |
Write-Host -NoNewline " (Error: $StatusCode)" | |
$FirstError=$false | |
} | |
} | |
if( $StatusCode -ne 200 -Or $response.content.contains($ResponseContentNoPresent)) { | |
sleep 1 | |
Write-Host -NoNewline "." | |
} else { | |
break | |
} | |
} | |
} | |
$SERVER_PATH = "/system/console/bundles.json" | |
$LOGIN = "admin:admin" | |
$SERVER_HOST = "localhost" | |
$PROTOCOL = "http" | |
$PORT = 4502 | |
$TIMEOUT = 5 | |
$ADDRESS = "${PROTOCOL}://${SERVER_HOST}:${PORT}" | |
WaitForUrlContent -ResponseContentNoPresent '"state":"Installed"' -Referer $ADDRESS -UserAgent "curl" -Url ${ADDRESS}${SERVER_PATH} -BasicAuthCreds $LOGIN -Timeout $TIMEOUT | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment