Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch script should send a mail for the error output  (Read 81236 times)

0 Members and 1 Guest are viewing this topic.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Batch script should send a mail for the error output
« Reply #15 on: September 04, 2020, 07:59:53 AM »
This script runs under Powershell Windows, uses Curl and I made an attempt at a Powershell version of the Blat command line:

Code: [Select]
$url = "url.txt"
$not_ok_url = "not_ok_url.txt"
$work = "response.txt"

[regex] $regex = @"
(?smi)(.*200\sOK)
"@

if ( Test-Path $not_ok_url ) { Remove-Item $not_ok_url }

Get-Content $url | foreach {
  $site = $_
  Start-Process -FilePath "curl.exe" -ArgumentList "-I -s $_" -NoNewWindow -RedirectStandardOutput $work -Wait
  $response = Get-Content $work -Raw
  if ( $response -match $regex) {
    Write-Output "$site ... OK"
  } else {
    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
    $response | Out-File -FilePath $not_ok_url -Append -encoding ascii
  }
  if ( Test-Path $work ) { Remove-Item $work }
}

# if ( Test-Path($not_ok_url) ) {
#   $sender =
#   $log =
#   $smtp =
#   Start-Process -FilePath "blat.exe" -ArgumentList "$sender -subject 'URLs DOWN STATUS' -body 'please check the attachment for the Application DOWN error logs' $smtp -attach 'C:\Not_OK_URL.txt' $log -to '[email protected]'
# }

You can tweak the paths at the top of the script. The Blat section is commented out but if remove the comment marks (#) and fill in the values for the variables, it just might work.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein