Solved

need powershell assistance with extracting an attachment from an inbox

Posted on 2014-10-22
4
199 Views
Last Modified: 2014-10-26
Hello, We need assistance with modifying the powershell code so that it will parse the subject line of the email, and put the attachment in a certain folder.


the shares will not match the subject line.

 so for example, the subject line of one email will be "bot1 capture metrics 1212121098"  and we need the attachment of that message to be copied to a specific share, in this case: \\domain.com\repositary\bot1-2014

 the second email may have a subject line of: "bot2 capture metrics 93438308903440343"  and we need the attachment of that message copied to a different specific share, in this case: \\domain.com\repositary\bot2-2014

this is the code we have so far:

$objOutlook   = New-Object -Com "Outlook.Application"
$objMailbox   = $objOutlook.GetNamespace("MAPI").Folders.Item("Mailbox - Docs")

$objMailbox.Folders.Item("Inbox").Items.Restrict("[UnRead] = True") | 
  select -Expand Attachments | % {
    for ($i = $_.Count; $i; $i--) {
      $_.Item($i).SaveAsFile("\\server\share\$($_.Item($i).FileName)")
      $_.Parent.Unread = $false
    }
  }

Open in new window

0
Comment
Question by:siber1
  • 2
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40398221
IMHO it's better to let Outlook do the work on receiving a mail.
The issue here is to create the rules for determining the target folder. If we can apply the pattern you showed, which is "botN capture ..." and the first word specifying the folder name (plus fixed suffix):
$objOutlook   = New-Object -Com "Outlook.Application"
$objMailbox   = $objOutlook.GetNamespace("MAPI").Folders.Item("Mailbox - Docs")

$objMailbox.Folders.Item("Inbox").Items.Restrict("[UnRead] = True") | 
  select -Expand Attachments | % {
    for ($i = $_.Count; $i; $i--) {
      $curFile = $_.Item($i)
      $curFile.SaveAsFile("\\server\share\$($curFile.SubstrIng(0,4))-2014\$($curFile.FileName)")
      $_.Parent.Unread = $false
    }
  }

Open in new window

It's not very robust, but you can use any expression using a RegEx if you like.
0
 

Author Comment

by:siber1
ID: 40398231
thanks Qlemo.

how can we target this script to one specific mailbox? sorry forgot to put that in my initial question.

S.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40398987
This script needs Outlook, and accesses the mailbox and folder as noted.
0
 

Author Closing Comment

by:siber1
ID: 40405286
thanks Qlemo, sorry for the delay in responding back. excellent script.

S.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

758 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

22 Experts available now in Live!

Get 1:1 Help Now