locked
automated PS script to grab outlook attachment RRS feed

  • Question

  • Hello all,

    I have a PS script which is supposed to search a mailbox for a specific attachment and save the file locally. Its not working for some reason and I was hoping someone could tell me what I'm missing...

    #file path
    $filepath = “C:\scripts\UltiProFeed\Filetoprocess\”
    
    
    #set outlook to open
    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace(“MAPI”)
    
    
    #you'll get a popup in outlook at this point where you pick the folder you want to scan
    $Account = $n.Folders | ? { $_.Name -eq 'email-removed' };
    $Inbox = $Account.Folders | ? { $_.Name -match 'Inbox' };
    $f = $Inbox.Folders | ? { $_.Name -match 'ultipro' };
    #date string to search for in attachment name
    #$date = (get-date).AddDays(-1).ToString('yyyyMMdd')
    #now loop through them and grab the attachments
    $f.Items | foreach {
        $_.attachments | foreach {
        Write-Host $_.filename
        $a = $_.filename
        If ($a.Contains('AD Export of Employee Information Report')) {
        $_.saveasfile((Join-Path $filepath $a))
          }
      }
    }
    
    ren "C:\scripts\UltiProFeed\Filetoprocess\AD Export of Employee Information report.csv" "AD Export of Employee Information Report.txt"

    Thanks in advance for any advice.

    Thursday, May 24, 2018 4:13 PM

All replies

  • $account = 'jsmith@outlook.com'
    $targetFolder = 'ultipro'
    $filepath = 'C:\scripts\UltiProFeed\Filetoprocess'
    $ol = New-Object -comobject outlook.application
    $ns = $ol.GetNamespace('MAPI')
    $acct = $ns.Folders[$account]
    $acct.Folders[$targetFolder].Items | 
        ForEach-Object{
            $_.attachments | 
                ForEach-Object{
                    Write-Host $_.filename
                    If ($_.filename -match 'AD Export of Employee Information Report'){
                        $_.saveasfile((Join-Path $filepath $_.FileName))
                    }
                }
        }


    \_(ツ)_/


    • Edited by jrv Thursday, May 24, 2018 5:00 PM
    Thursday, May 24, 2018 4:59 PM