Solved

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

Posted on 2016-07-19
3
29 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
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 12

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

13 Experts available now in Live!

Get 1:1 Help Now