Executing invoke-webrequest to get status of multple websites in Powershell
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 fileSet-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 alreadyif ([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 URLsforeach($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.
You should use an foreach for that because you have a list of websites. So you should try something like this.
$urls = Get-Content C:\webSiteAddress.txtforeach($url in $urls){Invoke-webrequest -URI $url |statuscode | export-csv c:\status.csv}