Solved

Read CVS, look specific value, then email

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

738 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