Solved

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

Posted on 2016-07-19
3
19 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
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
Comment Utility
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
Comment Utility
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 12

Expert Comment

by:Dustin Saunders
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now