Powershell: Extract attachments from unread emails and save to a network share

Hello everyone

I currently have a vbs script (below) checking a mailbox, searching for unread emails and saving the attachment to a network share.
I'm looking at changing this to be run by a powershell script.

Can anyone help me convert it? I'm running exchange 2010.

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objMailbox = objNamespace.Folders("Mailbox - Docs")
Set objFolder = objMailbox.Folders("Inbox")

Set colItems = objFolder.Items
Set colFilteredItems = colItems.Restrict("[UnRead] = True")

For Each objMessage in colFilteredItems
    intCount = objMessage.Attachments.Count
    If intCount > 0 Then
        For i = 1 To intCount
            objMessage.Attachments.Item(i).SaveAsFile "\\server\share\" &  _
                objMessage.Attachments.Item(i).FileName
        Next 
    End If
    objMessage.Unread = False
Next

Open in new window

LVL 4
techvagabondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

florin_sCommented:
Hi,

Since I do not have that much experience with vbscipts, I would suggest trying a converter, like this:

http://dmitrysotnikov.wordpress.com/2008/03/14/vbscript-to-powershell-converter/
0
prashanthdCommented:
Normal conversion from vbs to powershell will not work, you should use EWS.

http://gsexdev.blogspot.in/2010/01/writing-simple-scripted-process-to.html
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A simply transformation is (I've changed as little as possible, to allow for comparing):
$objOutlook   = New-Object -Com "Outlook.Application"
$objNamespace = $objOutlook.GetNamespace("MAPI")
$objMailbox   = $objNamespace.Folders.Item("Mailbox - Docs")
$objFolder    = $objMailbox.Folders.Item("Inbox")

$colItems         = $objFolder.Items
$colFilteredItems = $colItems.Restrict("[UnRead] = True")

ForEach ($objMessage in $colFilteredItems) {
    $intCount = $objMessage.Attachments.Count
    If ($intCount -gt 0) {
        For ($i=1; $i -le $intCount; $i++) {
            $objMessage.Attachments.Item($i).SaveAsFile("\\server\share\" +
                $objMessage.Attachments.Item($i).FileName)
        }
    }
    $objMessage.Unread = $false
}

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The optimized script looks like this:
$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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.