Solved

Email the results of an If-Then scenario

Posted on 2016-09-08
4
24 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:codedigger
  • 2
  • 2
4 Comments
 
LVL 39

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:codedigger
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 39

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:codedigger
ID: 41791625
Excellent, it worked great, I appreciate it, keep up the good work.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

23 Experts available now in Live!

Get 1:1 Help Now