Read emails in a shared mailbox using an admin account

Welcome Forums General PowerShell Q&A Read emails in a shared mailbox using an admin account

Viewing 2 reply threads
  • Author
    Posts
    • #213078
      Hil
      Participant
      Topics: 29
      Replies: 39
      Points: 184
      Rank: Participant

      I can use this script to read email in my own account. It works great. But if I specify an admin account (a4327) to read emails in another mailbox  $findResults still displays emails from my mailbox.

      How do i get the script to read emails from a different mailbox

      Here is my code:

      `<span class="pln">$Userid</span><span class="pun">=</span><span class="str">"a4327"</span><span class="pln">
      $pass_secure </span><span class="pun">=</span> <span class="typ">Get</span><span class="pun">-</span><span class="typ">Content</span> <span class="str">"e:\cred_a4327.txt"</span> <span class="pun">|</span> <span class="typ">ConvertTo</span><span class="pun">-</span><span class="typ">SecureString</span><span class="pln">
      $credential </span><span class="pun">=</span><span class="pln"> new</span><span class="pun">-</span><span class="pln">object </span><span class="pun">-</span><span class="pln">typename </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Management</span><span class="pun">.</span><span class="typ">Automation</span><span class="pun">.</span><span class="typ">PSCredential</span> <span class="pun">-</span><span class="pln">argumentlist $Userid</span><span class="pun">,</span><span class="pln"> $pass_secure
      $password </span><span class="pun">=</span><span class="pln"> $credential</span><span class="pun">.</span><span class="typ">GetNetworkCredential</span><span class="pun">().</span><span class="pln">password
      $dllpath </span><span class="pun">=</span> <span class="str">"C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"</span>
      <span class="pun">[</span><span class="pln">void</span><span class="pun">][</span><span class="typ">Reflection</span><span class="pun">.</span><span class="typ">Assembly</span><span class="pun">]::</span><span class="typ">LoadFile</span><span class="pun">(</span><span class="pln">$dllpath</span><span class="pun">)</span><span class="pln">
      $service </span><span class="pun">=</span><span class="pln"> new</span><span class="pun">-</span><span class="pln">object </span><span class="typ">Microsoft</span><span class="pun">.</span><span class="typ">Exchange</span><span class="pun">.</span><span class="typ">WebServices</span><span class="pun">.</span><span class="typ">Data</span><span class="pun">.</span><span class="typ">ExchangeService</span><span class="pln">
      $Service</span><span class="pun">.</span><span class="typ">Credentials</span> <span class="pun">=</span> <span class="typ">New</span><span class="pun">-</span><span class="typ">Object</span> <span class="typ">Microsoft</span><span class="pun">.</span><span class="typ">Exchange</span><span class="pun">.</span><span class="typ">WebServices</span><span class="pun">.</span><span class="typ">Data</span><span class="pun">.</span><span class="typ">WebCredentials</span><span class="pun">(</span><span class="pln">$Userid</span><span class="pun">,</span><span class="pln">$password</span><span class="pun">)</span><span class="pln">
      $TestUrlCallback = {
      param ([string] $url)
      if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
      }
      $Sharedmail </span><span class="pun">=</span> <span class="str">"MessagingReports"</span><span class="pln">
      $service</span><span class="pun">.</span><span class="typ">AutodiscoverUrl</span><span class="pun">(</span><span class="pln">$Sharedmail</span><span class="pun">,</span><span class="pln">$TestUrlCallback</span><span class="pun">)
      $PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
      $PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;
      $numOfEmailsToRead = 5
      $index = 0
      $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index)
      $findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)</span>`
      
    • #219768
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Do you have permissions for this other mailbox? I don’t even want to try and read through that code. Is there any specific reason you’re trying to get it from exchange web services instead of say Outlook?

    • #219792
      Hil
      Participant
      Topics: 29
      Replies: 39
      Points: 184
      Rank: Participant

      Do you have permissions for this other mailbox? I don’t even want to try and read through that code. Is there any specific reason you’re trying to get it from exchange web services instead of say Outlook?

      Its fixed. Thanks though.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.