Solved

need powershell assistance with extracting an attachment from an inbox

Posted on 2014-10-22
4
202 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

912 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

17 Experts available now in Live!

Get 1:1 Help Now