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
Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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