Hi I am new to PS.
I am executing SQL scripts via a batch file. PowerShell correctly exits the batch when a script fails. When that occurs I would like to send an email, or if no error occurs send email notice of completion.
I have successfully tested the email on completion part, but am having trouble with what to write for an IF ERROR scenario.
Here is what I have, any guidance is appreciated:
$ErrorActionPreference;
sqlcmd -b -S myserver -d mydatabase -i sql_test_fail.sql | Tee-Object -file "sql_test_fail.txt" -ErrorAction Stop;if ($lastExitCode -ne 0) {throw "Sqlcmd cmd failed."}
sqlcmd -b -S myserver -d mydatabase -i sql_test_pass.sql | Tee-Object -file "sql_test_pass.txt" -ErrorAction Stop;if ($lastExitCode -ne 0) {throw "Sqlcmd cmd failed."}
$emailSmtpServer = "smtp.myserver.com"
$emailFrom = "vree@someemail.com"
$emailTo = "vree@someemailcom"
$emailSubject = "Build e-mail"
$emailBody ="The Build 01 scripts have completed"
Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHTML -SmtpServer $emailSmtpServer