Solved

Email the results of an If-Then scenario

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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