Public/Get-UrlStatusCode.ps1
function Get-UrlStatusCode { # Stems from issue #24 (https://github.com/repasscloud/software-library/issues/24) # Returns the Status Code of a web URL that is passed in as an arg # Should always return a 200 result to be successful # List of URL status codes can be found here: https://www.w3.org/Protocols/HTTP/HTRESP.html # # Usage: # # $query = Get-URLStatusCode -Url https://www.google.com/ # $query -eq 200 # # Source: https://stackoverflow.com/a/20262872 param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ValueFromRemainingArguments=$false, HelpMessage='Provide URL to return HTTP Status Code.', Position=0)] [Alias('uri')] [Uri]$Url ) begin { # Set TLS 1.2 [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 } process { # First we create the request. $HTTP_Request = [System.Net.WebRequest]::Create($Url) # We then get a response from the site. $HTTP_Response = $HTTP_Request.GetResponse() # We then get the HTTP code as an integer. $HTTP_Status = [int]$HTTP_Response.StatusCode return $HTTP_Status # Finally, we clean up the http request by closing it. if ($null -eq $HTTP_Response) { } else { $HTTP_Response.Close() } } end { # Clean up memory [System.GC]::Collect() } } |