0

I am trying to write a powershell script to download a csv emailed daily save it to a folder, and then process it using powershell/excel. I have already written the powershell to open up the file and process it using a excel macro written in VBA, but I cannot seem to get this code below to download the attachment. The file name that is emailed is typically exceptions_123456.csv but filenames sometimes change, but its always .CSV and is the only csv file I get daily. so I want to download any csv file emailed to me on todays date sent to my inbox.
this is the code I have tried, it doesnt seem to work. I do not get any errors, I even tried replacing the $today variable with today's date manually "03/23/2023" and it wont download either. not sure where the issue is.

    Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\exceptions\"
$today = Get-Date -Format "MM/dd/yyyy"
$subfolder.Items | Where-Object {$_.receivedtime -match $today -and $($_.attachments).filename -match '.csv'} | foreach {
    foreach($attachment in $($_.attachments | where filename -match '.csv'))
    {
        Write-Host Downloading $attachment.filename to $filepath -ForegroundColor green
        $attachment.SaveAsFile((join-path $FilePath $attachment.filename))
        
    }
}

1 Answer 1

0

You have some issues with referencing the attachment and the $subfolder.Items not being accessible. Note - make sure outlook app is closed. Try this:

Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.getDefaultFolder($olDefaultFolders::olFolderInBox)
$FilePath= "c:\exceptions\"
$today = Get-Date -Format "MM/dd/yyyy"

$inboxitems = $inbox.Items | Where-Object {$_.receivedtime -ge $today -and $($_.attachments).filename -match '.csv'} 
foreach ($inboxitem in $inboxitems) { 
$name = $inboxitem.Attachments | Select Filename
Write-Host Downloading $name to $filepath -ForegroundColor green
        $($inboxitem.Attachments).SaveAsFile((join-path $FilePath $name.FileName))
        
    }
4
  • Thanks, will test this out and see if it works, although not having outlook open may be a problem in general. Mar 27, 2023 at 17:30
  • yeah, unfortunately it is a requirement of using the interop
    – Narzard
    Mar 27, 2023 at 17:36
  • Tested this out, but still not working, although it seemed to be doing something, but it ran way too long. powershell just hung up. but my inbox is very large. so going to try and rework this to download from a inbox subfolder with much less emails. Thanks for the guidance! Mar 27, 2023 at 17:37
  • Are your email accounts Office365 by chance? If so, there are some nice libraries that can rip attachments right from the exchange 365 webserver using graph api
    – Narzard
    Mar 27, 2023 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .