Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Email the results of an If-Then scenario

Posted on 2016-09-08
4
Medium Priority
?
46 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.
  • 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

WatchGuard Case Study: Museum of Flight

“With limited money and limited staffing, we didn’t have a lot of choices in terms of what we could do to bring efficiency. WatchGuard played a central part in changing that.” To provide strong, secure Wi-Fi access within the museum, Hunter chose to deploy WatchGuard’s AP120 APs.

Question has a verified solution.

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

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.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Loops Section Overview
Screencast - Getting to Know the Pipeline

926 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