Solved

need powershell assistance with extracting an attachment from an inbox

Posted on 2014-10-22
4
213 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
[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
  • 2
  • 2
4 Comments
 
LVL 70

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 70

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
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…

691 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