Solved

Read CVS, look specific value, then email

Posted on 2011-03-22
4
599 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
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 70

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

14 Experts available now in Live!

Get 1:1 Help Now