title: reading file attachments from Outlook - from a shared mailbox
body: <p>I am trying to transfer file attachments with a specific name from a folder in a shared mailbox, to a directory on my computer.</p>
  
  <p>My script is the following:</p>
  
  <pre><code>#file path
  $filepath = “c:\test”
  $account = "sharedMailbox@company.com"
  
  
  #date string to search for in attachment name
  $date = Get-Date -Format yyyyMMdd
  
  
  #set outlook to open
  $o = New-Object -comobject outlook.application
  $n = $o.GetNamespace(“MAPI”)
  
  $Account = $n.Folders | ? { $_.Name -eq $account };
      $f = $Account.Folders | ? { $_.Name -match 'Folder Containing Target Files' };
  
  
  
  #now loop through them and grab the attachments
  $f.Items | foreach {
      $_.attachments | foreach {
      Write-Host $_.filename
      $a = $_.filename
      If ($a.Contains($date)) {
      $_.saveasfile((Join-Path $filepath $a))
        }
    }
  }
  </code></pre>
  
  <p>I get the following error: <code>You cannot call a method on a null-valued expression.</code></p>
  
  <p>Now, when I remove the following block from the script, it does not produce an error, but it also does not log any filenames. This is wrong, because the subfolder does have an email that contains a file that satisfies the <code>$date</code> value:</p>
  
  <pre><code>$a = $_.filename
          If ($a.Contains($date)) {
          $_.saveasfile((Join-Path $filepath $a))
            }
  </code></pre>
  
  <p>This makes me think that I am not successfully connecting to the mailbox.</p>
  
  <p>Is there something that needs to be changed in the script that will allow me to successfully transfer files from this shared mailbox? I have read/write/delete permissions to the mailbox.</p>