-1

I have a set of URLs and I want to check if they are an actual site. I have tried this code :

$Uri = $Site -as [uri]
                                            if($Uri){
                                            write-host $Uri
                                            }

but when I put in a site like https://acasdev.sharepoidnt.com/sites/migrations which isn't a correct site it still outputs it out as valid

davetherave
  • 1,147
  • 3
  • 10
  • 15

1 Answers1

4

If you actually want to test there is a resource at that URL. Below will actually try to load it.

try
{ 
    # First we create the request.
    $HTTP_Request = [System.Net.WebRequest]::Create('https://acasdev.sharepoidnt.com/sites/migrations')   
    # 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
}
catch
{
    Write-Host "It's dead Jim!"
    exit
}

If ($HTTP_Status -eq 200) { 
    Write-Host "Site is OK!" 
}
Else {
    Write-Host "The Site may be down, please check!"
} 

# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()

Reference: Powershell Script to check the status of a URL

Community
  • 1
  • 1
Arcass
  • 932
  • 10
  • 19