Wednesday, April 19, 2017

PowerShell : BizTalk Error Send Email Alert.ps1

# biboyatienza - 13Jan.2017 15:59
# To Run => powershell -ExecutionPolicy Bypass -File "C:\BizTalk_Dropzone\PowerShellScripts\BizTalk Error Send Email Alert.ps1"
# biboyatienza - 19.April.2017 15:20 GMT+8 => Added detailed error:
##############################################################################
$SQLServer = "The_Server" #use Server\Instance for named SQL instances!
$SQLDBName = "IntegrationDb"
$SqlQuery = "EXEC [dbo].[usp_BizTalk_GetSuspended]"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

$DataSet.Tables[0]

foreach ($row in $DataSet.Tables[0].Rows)
{
    $ApplicationName = $row[0].ToString().Trim()
    $DateSuspended = $row[1].ToString().Trim()
    $Adapter = $row[2].ToString().Trim()
    $URI = $row[3].ToString().Trim()
    $ErrorDescription = $row[4].ToString().Trim()
    $Status = $row[5].ToString().Trim()

    # Email sending:
    $From = "NoReply <no_reply@biboyatienza.com>"
    $To = "admin@biboyatienza.com", "DBA <dba@biboyatienza.com>"
    $Bcc = "biboyatienza@gmail.com"
    $Subject = "Biztalk Error : " + $ApplicationName
    $Body = "Hi BizTalk Admin(s), `r`n`r`n"
    $Body = $Body + "Application Name: " + $ApplicationName + "`r`n"
    $Body = $Body + "Date Suspended: " + $DateSuspended + "`r`n"
    $Body = $Body + "Adapter: " + $Adapter + "`r`n"
    $Body = $Body + "URI: " + $URI + "`r`n"
    $Body = $Body + "Error Description: " + $ErrorDescription + "`r`n"
    $Body = $Body + "Status: " + $Status + "`r`n"
    $Body = $Body + "`r`n`r`n`r`n br,`r`n biboy atienza"
    $SMTPServer = "mail.biboyatienza.com"
    $SMTPPort = "25"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
    -Body $Body -SmtpServer $SMTPServer -port $SMTPPort
}
##############################################################################

No comments:

Post a Comment