Solved

Email the results of an If-Then scenario

Posted on 2016-09-08
4
36 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 40

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 40

Accepted Solution

by:
footech earned 500 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

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

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 brief introduction to what I consider to be the best editor for 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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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