Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-07-19
3
Medium Priority
?
50 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

664 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