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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

Read CVS, look specific value, then email

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
lost1000
Asked:
lost1000
1 Solution
 
GusGallowsCommented:
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
 
wls3Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now