Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####################################################################################
- # Author: William Reyor
- # opticfiber@gmail.com
- # Date:- 06/29/2015
- # Description:- read emailbody,extract attachment
- # Prerequisites :- Powershell/Outlook
- #
- #####################################################################################
- # Heavily borrowed from code and concepts from:
- # https://gallery.technet.microsoft.com/scriptcenter/Outlook-Automation-using-d7584688
- # http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/use-powershell-to-replace-text-in-strings.aspx
- # https://confidentialfiles.wordpress.com/2012/07/01/renaming-in-powershell-underscore-to-space/
- # http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/use-powershell-to-replace-text-in-strings.aspx
- # http://stackoverflow.com/questions/15113413/how-to-concatenate-strings-and-variables-in-powershell
- # http://www.computerperformance.co.uk/powershell/powershell_if_and.htm
- ###############################Debug Logs##################################################
- $date = get-date -format d
- $date = $date.ToString().Replace(“/”, “-”)
- $time = get-date -format t
- $time = $time.ToString().Replace(":", "-")
- $time = $time.ToString().Replace(" ", "")
- $log1 = ".\Logs" + "\" + "Processed_" + $date + "_.log"
- $logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt"
- # Start-Transcript -Path $logs
- $date1 = get-date
- #############################outlook Call#############################################
- $olFolderInbox = 6
- $outlook = new-object -com outlook.application;
- $ns = $outlook.GetNameSpace("MAPI");
- $inbox = $ns.GetDefaultFolder($olFolderInbox)
- $messages = $inbox.items
- write-host $messages.count
- $messcount = $messages.count
- add-content $log1 $date1
- add-content $log1 "Messages Count: $messcount"
- $countprocessed = 0
- foreach($message in $messages){
- $msubject = $message.subject
- add-content $log1 "Messages Subject: $msubject"
- $mBody = $message.body
- #Write-Host $mBody
- $mBodySplit = $mBody -split "Customer Email ID:"
- $toaddress1=$mBodySplit[1]
- $toaddress1
- add-content $log1 "Vendor Email: $toaddress1"
- ###################################Save Invoice#######################################
- # Change the subjet to a useable string to save to the file system
- $filepath = "c:\data\report\"
- $message.attachments|foreach {
- ## input cleanup from subjectlines
- $filename = $msubject -replace " ", "_"
- $filename = $filename -replace ":", "_"
- $attr = $_.filename
- add-content $log1 "Attachment: $attr"
- $a = $_.filename
- If ($a.Contains(".pdf") -and $filename.Contains("Vendor you get reports from or some other qualifying text") ) {
- $filename = $filename + ".pdf"
- Write-Host "filename:" + $filename
- $_.saveasfile((Join-Path $filepath $filename))
- }
- }
- $attachment = "c:\data\report\" + $filename + ".pdf"
- #added closing brocket -
- }
- ## Note using the rest of this code ##
- ########################send email invoice###########################################
- ##$mail = $outlook.CreateItem(0)
- ##$Mail.Recipients.Add($toaddress1)
- ##$Mail.Subject = "$msubject"
- ##$Mail.Body = ""
- ##$Mail.Attachments.Add($attachment)
- ##$Mail.Send()
- ##add-content $log1 "Message Sent: $toaddress1"
- #################################Move Items#########################################
- ##$MoveTarget = $inbox.Folders.item("Processed")
- ##[void]$message.Move($MoveTarget)
- ##add-content $log1 "Message Moved to: Processed"
- ##$countprocessed = $countprocessed + 1
- ##}
- ##add-content $log1 "Total Messages Processed: $countprocessed"
- ###########################Report Errors#############################################
- ##if ($error -ne $null)
- ##{
- ##$smtpServer = "SMTP Server"
- ##$msg = new-object Net.Mail.MailMessage
- ##$smtp = new-object Net.Mail.SmtpClient($smtpServer)
- ##$msg.From = "OutlookautomationScript@testlab.com"
- ##$msg.To.Add("Vikas.Sukhija@testlab.com")
- ##$msg.Subject = "Outlook Script Error"
- ##$msg.Body = $error
- ##$smtp.Send($msg)
- ##}
- ##
- ##Stop-Transcript
- #####################################END################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement