Solved

Modify Powershell script to only send Send-MailMessage if database condition is met.   Column [Acative] = 'Active'

Posted on 2016-07-19
3
43 Views
Last Modified: 2016-07-19
I have a nicely working Powershell script (Thank you Qlemo!) that I've put into a scheduled task to run every day and send an email to specified staff if ContractDate is within 15, 30, 60, or 90 days.  All that's working great.

The database also contains a field for these contracts, [Active], which may contain the string, "Active" or "Not active" depending on... you know.

How would I add, "If $_.Active = "Active" go ahead and send the email, otherwise don't do anything."

<# connection string and SQL command to execute #>
[String] $constr = 'Server=aSQLServer; Database=Contracts; Integrated Security=SSPI;'
[String] $sqlCmd = @'
  select Company, datediff(day, getdate(), ExpirationDate) as Diff, email01, email02, email03, email04, email05, ContactPerson, Notes, Active
    from Contracts
   where datediff(day, getdate(), ExpirationDate) in (15,30,60,90)
'@


$set = New-Object System.Data.Dataset
(New-Object System.Data.SqlClient.SqlDataAdapter ($sqlCmd, $constr)).Fill($set) | out-null

$set.Tables[0] |
  % {
    $to = $_.eMail01, $_.eMail02, $_.eMail03, $_.eMail04, $_.eMail05 | ? { $_ -ne '' -and !($_ -is [DBNull]) }
    <# Mail parameters #>
    Send-MailMessage -BodyAsHtml -Body "<span style='color:red; font-size:24px;'>Please be aware that contract / agreement: </span><span style='color:black; font-size:32px;'> $($_.Company)</span><span style='color:red; font-size:24px;'> will expire in $($_.Diff) days</span><br /><br /><span style='color:blue;'>CSBD Primary Contact: $($_.ContactPerson)</span><br /><br /><span style='color:green;'>Contract Renewal Notes: <br />$($_.Notes)</span>" -SmtpServer mail.careersourcebroward.com -From "Contract Expiration Database <csbd@careersourcebroward.com>" -To $to -Subject "Contract for $($_.Company) will expire in $($_.Diff) days"
  }

Open in new window

0
Comment
Question by:megnin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41719380
You just need to add a condition like:

if ($_.Active -eq "Active")
{
  $to = $_.eMail01, $_.eMail02, $_.eMail03, $_.eMail04, $_.eMail05 | ? { $_ -ne '' -and !($_ -is [DBNull]) }
    <# Mail parameters #>
    Send-MailMessage -BodyAsHtml -Body "<span style='color:red; font-size:24px;'>Please be aware that contract / agreement: </span><span style='color:black; font-size:32px;'> $($_.Company)</span><span style='color:red; font-size:24px;'> will expire in $($_.Diff) days</span><br /><br /><span style='color:blue;'>CSBD Primary Contact: $($_.ContactPerson)</span><br /><br /><span style='color:green;'>Contract Renewal Notes: <br />$($_.Notes)</span>" -SmtpServer mail.careersourcebroward.com -From "Contract Expiration Database <csbd@careersourcebroward.com>" -To $to -Subject "Contract for $($_.Company) will expire in $($_.Diff) days"
}

Open in new window


Only if the condition is met will it execute the code in brackets, otherwise it ignores.
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 41719455
Thank you, Dustin!  I came up with something close to that, that still worked, though not really correct.  ;-)

if($($_.Active) -eq "Active") seems to also work.  I'll fix my script since your solution is more correct.

The first thing I tried was using the "=" operator instead of "-eq."  That gave me an error, so I turned to Google for some examples.  None had the database parameters that I'm using, so I didn't get that quite right.

Thanks again!
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41719467
No worries, and yeah I've spent time flustered at non functional powershell only to find I used '=' instead of -eq which meant it was always true!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question