Solved

need powershell assistance with extracting an attachment from an inbox

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

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 69

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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