2

I have created an DB refresh job on a pre-prod SQL server, with latest successful Prod backup to refresh pre-prod DB every monday morning. However, i want to send a status mail post refresh. Due to compliance and security risks in our financial company, we can not send mails from pre-prod server. I was able to send mails successfully from Prod.

Is there any method to send mail with DB refresh status. My thoughts are 1. Refresh script writes the log to a folder in Dev server - should i write a windows batch/powershell script in Prod server to access the folder in dev and read the file to send a mail post refresh with the status? 2. I can not use any linked server from Pre-prod to Prod.

Any other thoughts?

  • This seems to be reversed, you can send emails from production but not from development? – kevinsky Jul 15 '19 at 18:27
  • I know it's reverse scenario here...unfortunately!!!....Yes... – Kris Jul 15 '19 at 18:44
  • I don't get why you can't send dbmail from pre-prod, but your first idea of writing to a log file, and then scheduling a ps or batch job to send an email with the log contents could be a viable option. – scsimon Jul 15 '19 at 19:13
  • Since this seems to be a security related thing, whatever you come up with should be run by the security team. That said, other than the save to disk (probably a shared common drive would be better, not on the SQL Server itself) and send, if there is another machine that has mailing privileges and can run a query from against the Dev machine to generate the report (via linked server for example) and mail it from there, that would be an easier solution than dump to disk and send. – Laughing Vergil Jul 15 '19 at 19:27
  • Can you have a linked server from Prod to PreProd? – Conrad S. Jul 16 '19 at 4:23
1

So, I used the below script.

Prerequisites are that the refresh script will write the log to a shared location accessible from a Prod Server.

$date = Get-Date -Format d
$dir = "\\xxxxx\g$\QARefreshLogs"
$recipient = "xxxxxx" 
$smtpserver = "xxxxxxx" 
$smtpport = "xx"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg = new-object Net.Mail.MailMessage 
$msg.From = "xxxxxxxxxx"
$msg.To.Add($recipient)

---Retrieving the latest file 

$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Where-Object {$_.Name -ilike "<db_name>_refresh_log_*"} | Select-Object -First 1
$LatestRefreshLog = $latest.Name
$LatestRefreshLogDate = $latest.LastWriteTime.ToString('M/dd/yyyy')

--Validating if the log file generated today?

if (($LatestRefreshLogDate) -notmatch $date)
     {
      #Write-Host "WARNING: There is no today's restore log file!"
      $msg.Subject = "SQLServer QA Refresh Status" 
      $msg.Body = "QA MSSQL DB refresh is having issues.Please validate."
      $smtp.Send($msg)
     }
     else
     {

--Validating if refresh is success or not?

      If(Get-Content -Path $dir\$LatestRefreshLog | %{$_ -match "RESTORE DATABASE successfully processed"})
      {
       #Write-Host "The latest restore log file is $LatestRefreshLog"
       $msg.Subject = "SQLServer QA Refresh Status" 
       $msg.Body = "QA DB is refreshed successfully."
       $smtp.Send($msg)
      }
      else
      { 
       #Write-Host "The latest restore log file is $LatestRefreshLog"
       $msg.Subject = "SQLServer QA Refresh Status" 
       $msg.Body = "QA DB refresh is having issues.Restore failed."
       $smtp.Send($msg)
     } }

This may not be an optimal script. It served my purpose....thanks for all your inputs. This script could be helpful to others, have a PoSh time!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.