Dave
Dave

Reputation: 1

Using PowerShell to save outlook attachments

I'm struggling to find a solution so hoping somebody may have encountered this. I have a PowerShell script to save any Outlook attachments to automate a daily check. This works fine running it manually, however it's run overnight on a desktop so it fails due to the device being locked.

I know excel works by setting its visibility to false, but can't find a similar option for outlook. Any help will be greatly appreciated!

$outlook = New-Object -comobject outlook.application
$inbox = $outlook.GetNamespace("MAPI")
$find = $inbox.GetDefaultFolder(6)

$filepath = "$InputDir"
$find.Items| foreach {
 $SendName = $_.SenderName
   $_.attachments|foreach {
    Write-Host $_.filename
    $name = $_.filename
    If( -Not (Test-Path -Path "$InputDir\$name")) {
    If ($name.Contains("txt")) {
    $_.saveasfile((Join-Path $filepath "$name"))
       }
    }
  }
 }

Upvotes: 0

Views: 6142

Answers (1)

supermerio
supermerio

Reputation: 240

You should use EWS (Exchange Web Services).

This a good example on how to query Exchange directly (without Outlook) to retrieve items. It is a little more complicated although worth it as it can be run as a scheduled task on a machine that isn't even logged on:

https://gsexdev.blogspot.com/2010/01/writing-simple-scripted-process-to.html

Upvotes: 0

Related Questions