Solved

Read CVS, look specific value, then email

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

744 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