Solved

Read CVS, look specific value, then email

Posted on 2011-03-22
4
616 Views
Last Modified: 2012-05-11
I need to create a powershell script that read a .csv file, look for a specific value on column 5 and 6 if higher than 10 on or 1000 on 6... send an email with the info in the all line, then copy a file into a directory

Message      ID      Name      Created                         Unique             Total  
XXXX      1      Unknown      01/10/2011 23:22:11:001      8      1360
XXXX      2      Unknown 2      01/10/2011 23:22:11:000      1      1
0
Comment
Question by:lost1000
[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
4 Comments
 
LVL 12

Accepted Solution

by:
GusGallows earned 125 total points
ID: 35193678
This should do the trick. You will need to know the address of an SMTP server that will let you relay the email through it.
#function to send email
function Send-SMTPmail($to, $from, $subject, $body, $attachment, $cc, $bcc, $port, $timeout, $smtpserver, [switch] $html, [switch] $alert) {
    if ($smtpserver -eq $null) {$smtpserver = "SMTPSERVER"} #put your email server in for the SMTPSERVER
    $mailer = new-object Net.Mail.SMTPclient($smtpserver)
    if ($port -ne $null) {$mailer.port = $port}
    if ($timeout -ne $null) {$mailer.timeout = $timeout}
    $msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
    if ($html) {$msg.IsBodyHTML = $true}
    if ($cc -ne $null) {$msg.cc.add($cc)}
    if ($bcc -ne $null) {$msg.bcc.add($bcc)}
    if ($alert) {$msg.Headers.Add("message-id", "<3bd50098e401463aa228377848493927-1>")}
    if ($attachment -ne $null) {
        $attachment = new-object Net.Mail.Attachment($attachment)
        $msg.attachments.add($attachment)
    }
    $mailer.send($msg)
}
#put in email addresses to send and receive message
$sFrom = "application@somewhere.com"
$sTo = "dude@somewhere.com"

#put in subject of email
$sTitle = "Report"

#create an output file and write a header to it
$out = "c:\output.txt" #make this wherever you want the file to write to
out-file $out -inputobject "Report" #Make "Report" whatever you want the header to be.

#read in CSV file
$file = import-csv "c:\filename.csv"

#go through each line of the csv and check the logic to see if it has the value you are looking for
foreach ($line in $file)
{
	$col5 = $line."Unique"
	$col6 = $line."Total"
	If (($col5 -gt 10) -or ($col6 -gt 1000))
	{
		$sBody = $line
		Send-SMTPmail -to "$sTo" -from "$sFrom" -subject "$sTitle" -body "$sBody" -html
		out-file $out -inputobject $line -append
	}
}

Open in new window

0
 
LVL 10

Expert Comment

by:wls3
ID: 35205007
This assumes you want to send an email and copy a file for every individual line you find matching these values:
Function ExamineCSVAndNotify($csvFilePath, $from, $to, $subject, $copyFilePath)
{
    foreach($line in $csvFilePath)
    {
        if(($line.Unique -gt 10) -OR ($line.Total -gt 10000))
        {
            Send-MailMessage -From $from -To $to -Subject $subject -Body $line # put the rest of your options here as needed.  Use Get-Help Send-MailMessage for more information on this cmdlet.
            Copy-Item -Path $csvFilePath -Destination $copyFilePath # this assumes the input file it what you want to copy
        }
    }
}

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35687989
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

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