title: Targeting shared mailbox with powershell to extract attachments
body: <p>I am relatively new to powershell and I have a bit of a conundrum.  I have a script that a friend of mine helped me modify which goes through my email and looks for MSG attachments, extracts them to a temp folder, extracts an xls attachment from each, and converts it to a csv file (saving me insane amounts of time).  It works really well, as a whole, but it is targeting my default inbox and I want to modify it to target a shared mailbox instead.</p>
  
  <p>this is the current outlook call;</p>
  
  <pre><code>$olFolderInbox = 6 
  $outlook = new-object -com outlook.application; 
  $ns = $outlook.GetNameSpace("MAPI"); 
  $inbox = $ns.GetDefaultFolder($olFolderInbox) 
  $messages = $inbox.items
  </code></pre>
  
  <p>Which I adapted from Sukhija Vikas Here(<a href="https://gallery.technet.microsoft.com/office/Outlook-Automation-using-d7584688" rel="nofollow">https://gallery.technet.microsoft.com/office/Outlook-Automation-using-d7584688</a>)</p>
  
  <p>I want to modify it further to look at a specific mailbox (ex: reports@company.com) so that I can have it get more messages than the ones that were accidentally sent to my primary email address.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>here is the whole script for reference;</p>
  
  <pre><code>##################################################################################### 
  #             Author: Vikas Sukhija 
  #             Date:- 09/12/2013 
  #                       Description:- read emailbody,extract attachment &amp; send email  
  #            with extracted attachment 
  #                       Prerequisites :- Powershell/Outlook 
  ##################################################################################### 
  import-module msgutility 
  ###############################Logs################################################## 
  &lt;#$date = get-date -format d 
  $date = $date.ToString().Replace(“/”, “-”) 
  $time = get-date -format t 
  #$time = $time.ToString().Replace(":", "-") 
  $time = $time.ToString().Replace(" ", "") 
  
  #$log1 = ".\Logs" + "\" + "Processed_" + $date + "_.log" 
  
  #$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" 
  
  Start-Transcript -Path $logs  
  
  $date1 = get-date 
   #&gt;
  #############################outlook Call############################################# 
  $olFolderInbox = 6 
  $outlook = new-object -com outlook.application; 
  $ns = $outlook.GetNameSpace("MAPI"); 
  $inbox = $ns.GetDefaultFolder($olFolderInbox) 
  $messages = $inbox.items 
  write-host $messages.count 
  $messcount = $messages.count 
  #add-content $log1 $date1 
  #add-content $log1 "Messages Count: $messcount" 
  $countprocessed = 0 
  foreach($message in $messages){ 
  $msubject = $message.subject 
  #add-content $log1 "Messages Subject: $msubject" 
  $mBody = $message.body 
  #Write-Host $mBody 
  $mBodySplit = $mBody -split "Customer Email ID:" 
  $toaddress1=$mBodySplit[1] 
  $toaddress1 
  #add-content $log1 "Vendor Email: $toaddress1" 
  
  ###################################Save Invoice####################################### 
  
  $filepath = "c:\temp\" 
  if ( $msubject -eq "Open/Closed Reports from iClose")
  {
  $message.attachments|foreach { 
      Write-Host $_.filename 
      $attr = $_.filename 
      #add-content $log1 "Attachment: $attr" 
      $a = $_.filename 
      If ($a.Contains("msg")) { 
      $_.saveasfile((Join-Path $filepath $a)) 
                               } 
    } 
  $attachment = "c:\temp\" + $a 
  }
  
  
  }
  set-location C:\Temp
  Expand-MsgAttachment *
  
  $xls = Get-ChildItem *.xls
  foreach ($item in $xls)
  {
  ExportWSToCSV -excelFileName $item.FullName
  } 
  </code></pre>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>I have changed my outlook call to this;</p>
  
  <pre><code>$olFolderInbox = 6 
  $outlook = new-object -com outlook.application; 
  $namespace = $outlook.GetNameSpace("MAPI"); 
  $recipient = $namespace.CreateRecipient("reports@mainspringservices.com")
  $inbox = $namespace.GetSharedDefaultFolder($olFolderInbox) 
  $messages = $inbox.items 
  write-host $messages.count 
  $messcount = $messages.count 
  #add-content $log1 $date1 
  #add-content $log1 "Messages Count: $messcount" 
  $countprocessed = 0 
  foreach($message in $messages){ 
  $msubject = $message.subject 
  #add-content $log1 "Messages Subject: $msubject" 
  $mBody = $message.body 
  #Write-Host $mBody 
  $mBodySplit = $mBody -split "Customer Email ID:" 
  $toaddress1=$mBodySplit[1] 
  $toaddress1 
  </code></pre>
  
  <p>But am now getting this error:</p>
  
  <pre><code>Cannot find an overload for "GetSharedDefaultFolder" and the argument count: "1".
  At C:\Users\Tyler\Documents\Windowspowershell\Powershell files\get-csvfromemail.ps1:30 char:1
  + $inbox = $namespace.GetSharedDefaultFolder($olFolderInbox)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodCountCouldNotFindBest
  
  0
  Load : The term 'Load' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path 
  is correct and try again.
  At C:\Users\Tyler\Documents\WindowsPowerShell\Modules\msgutility\msgutility.psm1:19 char:9
  +         Load application
  +         ~~~~
      + CategoryInfo          : ObjectNotFound: (Load:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>