I currently have a script set up that runs every hour and looks up the value of an order is over 100,000 in my SQL database. if the query returns with a result it emails a certain user letting them know the details of the order. The only problem is that it sends a blank email if no results are found. How can I change this to only send an email when there are rows in the query?
My current script looks like this;
$SqlServer = MySQLserver
$SqlCatalog = MyDB
$SqlQuery = select TotalCost from OrderTable
where TotalCost >= 100000
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = Server = $SqlServer; Database =
$SqlCatalog; 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]
$results = $DataSet.Tables | format-table -autosize | out-string
$body1 =$results
$emailFrom = SQLserver@mycompany.com
$emailTo = TheMainMan@mycompany
$subject = Order Over 100,000
$emailbody = $body1
$message = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $emailbody)
$smtpServer = myMailserver
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
###