Search code examples
powershellurliisweb-administrationhttp-status-code-200

Executing invoke-webrequest to get status of multple websites in Powershell


We have several websites and I don't want to check the status in powershell using invoke-webrequest -URI google.com every time.

I drafted something like this, which isn't working and I'd like help with:

$URLS = get-content c:\websitesaddress.txt
Invoke-webrequest -URI $urls |statuscode | export-csv c:\status.csv

I would like to check the status of several webpages and export it as CSV in the format website name, statuscode.


Solution

  • You're not far away from the answer, you just needed to get the statuscode property of the Invoke-WebRequest call, like so:

    Invoke-webrequest -URI $url | select statuscode
    

    or

    $(Invoke-webrequest -URI $url).statuscode
    

    And if that were to be piped into the Export-Csv cmdlet you'd only get the statuscodes, and not the url, plus the file would need to be appended to on each loop, as it stands the file would be recreated each time, only displaying the statuscode for the very last url in the file.

    I've shown two possible approaches below:

    Simple file write

    This approach isn't exactly the cleanest, it doesn't use the Export-Csv cmdlet

    $urls = Get-Content C:\webSiteAddress.txt
    
    # Add the header to the file
    Set-Content "C:\status.csv" -Value "URL,Status"
    foreach($url in $urls)
    {
        $status = $(Invoke-webrequest -URI $url).statuscode
        # Create the data line
        $line = "$url,$status"
        # Append the data to the file
        Add-Content "C:\status.csv" $line
    }
    

    Export and append to CSV

    Using Export-Csv is a little more complicated because Export-Csv expects and object with properties as its columns. On each loop, you tell the process to -Append to the existing CSV so the data isn't lost in the process.

    # Remove the file if it exists already
    if ([System.IO.File]::Exists("C:\status.csv")) {Remove-Item "C:\status.csv"}
    
    # Get the web addresses
    $urls = Get-Content "C:\webSiteAddress.txt" 
    
    # Loop over the URLs
    foreach($url in $urls) {
        # Create a new object containing the url and the statuscode property
        # of an Invoke-WebRequest call. Choose the column names here by changing url,status
        New-Object PSObject -Property @{
        url=$url;
        status=$(Invoke-webrequest -URI $url).statuscode
        } | 
        export-csv "C:\status.csv" -Append -NoTypeInformation
    }
    

    The second approach is preferable because it properly quotes the values in the CSV, preventing any data integrity issues.