I came up with this. If you want files in a certain folder, change the first two lines accordingly.
$url = "url.txt"
$not_ok_url = "not_ok_url.txt"
Get-Content $url | foreach {
$site = $_
try {
$response = Invoke-WebRequest $_
Write-Output "$site OK"
} catch {
Write-Output "$site NOK"
("-" * 75) | Out-File -FilePath $not_ok_url -Append -encoding ascii
$site | Out-File -FilePath $not_ok_url -Append -encoding ascii
("-" * 75) | Out-File -FilePath $not_ok_url -Append -encoding ascii
$_.Exception | Out-File -FilePath $not_ok_url -Append -encoding ascii
}
}
if ( Test-Path($not_ok_url) ) {
Invoke-Expression -Command "cmd.exe blat.bat"
}
This will run in both Powershell .Net Framework (5.1) and Powershell .Net Core (7.3). Now the bad news. The
Send-MailMessage cmdlet no longer works in either Powershell version. You can use the
MailKit module in Powershell Core but it's not pretty and I only got it to work with Gmail. Instead I suggest you write a
blat.bat file to set your variables and run blat as usual in a
cmd environment. I set that up in the Powershell script, but you'll have to write the code for the blat batch file.
Hope this helps
PS. Curl is an alias for Invoke-Webaddress.