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

x
?
Solved

Email the results of an If-Then scenario

Posted on 2016-09-08
4
Medium Priority
?
41 Views
Last Modified: 2016-09-09
I got this powershell script that consists of a Query Part and an Email Part, independently the two parts work fine, but jointly I'm having problem getting them to work properly, in the Query part, I'm saying that if a network file is generated with today's date, send out an email saying "success", else send out an email saying "failed".

that's the extent of it, please advise.
Thx
0
Comment
Question by:Roberto Madro R.
[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
  • 2
4 Comments
 
LVL 41

Expert Comment

by:footech
ID: 41790521
Provide the code, please.  It'll be much easier to point out what's required and where.
0
 

Author Comment

by:Roberto Madro R.
ID: 41790527
$dir = "C:\FileLocation\FTPVerification"
$latest = Get-ChildItem -Path $dir | sort LastWriteTime | Select-Object -Last 1
#$latest.name
$File = "C:\FileLocation\FTPVerification\*.csv"
IF($File.LastWriteTime -eq $currentdate)
{Write-host "$latest This EPL File was generated and FTP'd to Vendor today"}
Else
{"No EPL was generated today, please run the process manually"}
# Configure SMTP server
$smtpServer = "10.x.x.x"
$mailMessage = new-object Net.Mail.MailMessage
$smtpObj = new-object Net.Mail.SmtpClient($smtpServer)

# Set email parameters
$mailMessage.From = "email@company.org"
$mailMessage.ReplyTo = "email@company.org"
$mailMessage.To.Add("vendor@vendor.org")
$mailMessage.subject = "EPL FTP Status"
$mailMessage.body = "This   $latest   EPL File was generated and FTP'd to CTS today"
$mailMessage.Attachments.Add($attachment)

# Send email
$smtpObj.Send($mailMessage)
$attachment.Dispose()
0
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 41790543
Here's my take on it.
# Set common email parameters
$MessageParams = @{
    From = "email@company.org"
    To   = "vendor@vendor.org"
    Subject = "EPL FTP Status"
    SmtpServer = "10.x.x.x"
    }

$dir = "C:\FileLocation\FTPVerification" #If you're just interested in .CSV files, modify this path.
$latest = Get-ChildItem -Path $dir | sort LastWriteTime | Select-Object -Last 1
$currentdate = (Get-Date).Date
If ($latest.LastWriteTime -gt $currentdate)
{
    Write-host "$($latest.name): This EPL File was generated and FTP'd to Vendor today" -ForegroundColor Yellow
    $MessageParams["Body"] = "Success.  An EPL file was generated and is attached."
    $MessageParams["Attachment"] = $latest
}
Else
{
    Write-Host "No EPL was generated today, please run the process manually" -ForegroundColor Yellow
    $MessageParams["Body"] = "Failed"
}

Send-MailMessage @MessageParams

Open in new window


I prefer to utilize the Send-MailMessage cmdlet vs. using .NET classes unless it's not possible.
0
 

Author Closing Comment

by:Roberto Madro R.
ID: 41791625
Excellent, it worked great, I appreciate it, keep up the good work.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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