PowerShell script to check the status of a URL

后端 未结 6 668
不知归路
不知归路 2020-12-04 18:36

Similar to this question here I am trying to monitor if a set of website links are up and running or not responding. I have found the same PowerShell script over the Interne

6条回答
  •  星月不相逢
    2020-12-04 19:15

    I recently set up a script that does this.

    As David Brabant pointed out, you can use the System.Net.WebRequest class to do an HTTP request.

    To check whether it is operational, you should use the following example code:

    # First we create the request.
    $HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
    
    # 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
    
    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.
    If ($HTTP_Response -eq $null) { } 
    Else { $HTTP_Response.Close() }
    

提交回复
热议问题