I wanted a basic function that checks that a URL is valid. So I created the following:
function IsValidUrl([string] $url)
{
# Declare default return value
$isValid = $false
try
{
# Create a request object to "ping" the URL
$request = [System.Net.WebRequest]::Create($url)
$request.Method = "HEAD"
$request.UseDefaultCredentials = $true
# Capture the response from the "ping"
$response = $request.GetResponse()
$httpStatus = $response.StatusCode
# Check the status code to see if the URL is valid
$isValid = ($httpStatus -eq "OK")
}
catch
{
# Write error log
Write-Host $Error[0].Exception
}
return $isValid
}
No comments:
Post a Comment