locked
Checking status of a website error RRS feed

  • Question

  • Hello, we are currently using an internal T&E system and have had problems with it sometimes being down. I found a script but it looks like I need to be an authorized user. Not sure if I can input my credentials. It doesn't have to be exactly the same script if you have a different one that might work better. The error I am getting is -
    Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (401) Unauthorized."
    At line:8 char:4
    +    $output = $webClient.DownloadString("http://XXXXX...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : WebException

    $webClient = new-object System.Net.WebClient
    $webClient.Headers.Add("user-agent", "PowerShell Script")
    
    while (1 -eq 1) {
       $output = ""
    
       $startTime = get-date
       $output = $webClient.DownloadString("http://XXXX.com")
       $endTime = get-date
    
       if ($output -like "*Tim Johnson*") {
          "Success`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"
       } else {
          "Fail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"
       }
    
       sleep(300)
    }

    Tuesday, February 10, 2015 7:31 PM

Answers

  • $response=Invoke-WebRequest https://www.google.com
    if($response.StatusCode -eq 200){
        # OK
    }else{
       # page error
    }

    YOu need credentials on the site you are tesing so add the credential:

    $response=Invoke-WebRequest https://www.google.com -Credential $cred


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, February 10, 2015 8:27 PM
    • Marked as answer by bohlingj Tuesday, February 10, 2015 9:16 PM
    Tuesday, February 10, 2015 8:26 PM

All replies

  • $response=Invoke-WebRequest https://www.google.com
    if($response.StatusCode -eq 200){
        # OK
    }else{
       # page error
    }

    YOu need credentials on the site you are tesing so add the credential:

    $response=Invoke-WebRequest https://www.google.com -Credential $cred


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, February 10, 2015 8:27 PM
    • Marked as answer by bohlingj Tuesday, February 10, 2015 9:16 PM
    Tuesday, February 10, 2015 8:26 PM
  • I get the same error - 
    }
    Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
    At line:1 char:11
    + $response=Invoke-WebRequest "http://www.xxxxx
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    Tuesday, February 10, 2015 8:28 PM
  • I am no longer receiving an error but nothing is happening. 
    Tuesday, February 10, 2015 8:45 PM
  • I am no longer receiving an error but nothing is happening. 

    I have no idea what you are saying.   If you successfully connect you will receive a response object.  It is in the variable and nothing is displayed.  You need to inspect the statuscode as demonstrated above..


    ¯\_(ツ)_/¯

    • Marked as answer by bohlingj Tuesday, February 10, 2015 9:16 PM
    • Unmarked as answer by bohlingj Tuesday, February 10, 2015 9:16 PM
    Tuesday, February 10, 2015 8:51 PM
  • I am very sorry, I understand now. To make it a little more safe I actually added a $cred | Export-CliXml -Path C:\users\home\doc.XML to save it as encrypted. That worked so I am good to go!
    Tuesday, February 10, 2015 9:16 PM