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.
You're not far away from the answer, you just needed to get the statuscode property of the Invoke-WebRequest call, like so:
or
And if that were to be piped into the
Export-Csvcmdlet 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-CsvcmdletExport and append to CSV
Using
Export-Csvis a little more complicated becauseExport-Csvexpects and object with properties as its columns. On each loop, you tell the process to-Appendto the existing CSV so the data isn't lost in the process.The second approach is preferable because it properly quotes the values in the CSV, preventing any data integrity issues.