Private/CheckUrlStatus.ps1
function CheckUrlStatus { [CmdletBinding()] param( [parameter(Mandatory=$True)] [string]$SiteUrl ) if ($PSVersionTable.PSEdition -eq 'Core') { try { $HttpClient = [System.Net.Http.HttpClient]::new() $HttpClient.Timeout = [timespan]::FromSeconds(10) $HttpResponse = $HttpClient.GetAsync($SiteUrl) $i = 0 while ($(-not $HttpResponse.IsCompleted) -and $i -lt 10) { Start-Sleep -Seconds 1 $i++ } if ($HttpResponse.Status.ToString() -eq "Canceled") { throw "Cannot connect to '$SiteUrl'! Halting!" } $True } catch { Write-Error $_ return } } else { $Request = [System.Net.WebRequest]::Create($SiteUrl) try { $Response = $Request.GetResponse() $Status = $Response.StatusCode if ($Status) {$True} } catch { if ($_.Exception.Message -match '400') { $True } else { Write-Error $_ return } } } } |