PS Invoke-WebRequest Test Multiple Websites

Razzi29 336 Reputation points
2022-10-20T12:07:23.803+00:00

I am hoping someone has tried this before and can assist me here! I am trying to automate using Powershell a way to test the responses from a list of websites, let's say as an example I want to script to check/ test five sites if there are available by using the PS "Invoke-WebRequest" cmdlet. But here is the logic function I need help with, is that I need to Invoke-WebRequest a site, check for status 200 then go to sleep for 10 seconds, then check the next one, but if the return if other than 200 or not available I need to build a function that does something else when the site is not available or responding. In addition, as the scripts work their way through the functions to test sites, I need to record the results in text or csv format. Appreciate any assistance. Thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Andreas Baumgarten 129.4K Reputation points MVP Volunteer Moderator
    2022-10-20T12:47:15.777+00:00

    Hi @Razzi29 ,

    maybe this helps to get started:

    $urls = "https://www.google.deu" , "www.google.com", "www.microsoft.com"  
    $resultFile = "result.txt"  
    $urls | ForEach-Object { $result = ""  
        $result = Invoke-WebRequest -Uri $_  
        if (($result.StatusCode) -eq 200) {  
            Write-Output "Hurray"  
            Out-File -FilePath $resultFile -Encoding utf8 -Append -InputObject "$_ test sucess"  
            Start-Sleep 10  
        }  
        else {  
            Write-Output "Oh nooooo .... Do something else"  
            Out-File -FilePath $resultFile -Encoding utf8 -Append -InputObject "$_ test failed"  
        }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


1 additional answer

Sort by: Most helpful
  1. Newbie Jones 1,401 Reputation points
    2022-10-20T12:53:58.99+00:00

    I doubt anyone will write you a script to do this.

    You need to attempt this first, and if you have issues, post your code, and then someone will more likely help you with the script.

    Basic logic is as follows.

    Store the five sites into a variable.
    Create a foreach loop using the variable.
    In the foreach loop, send the invoke-webrequest and an IF statement checking for the status storing the results into another variable\object.
    It's within this IF statement you can add extra logic to do something else if it fails. (Try\Catch).
    Finally, output the variable\object storing the results as a CSV file.

    It sounds like you need to learn the basics in PowerShell in the first instance.

    The following download is free and will explain how to create loops, if statements, and export to CSV.
    https://www.sapien.com/books_training/Windows-PowerShell-4

    Start with the first half of the book and do the exercises to cover the basics.
    Then work through the 2nd half of the book which covers common tasks.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.