Author Posts

January 26, 2016 at 12:18 pm

What I would like to do is retrieve the email for a specific user(generic account). I'm not sure if that's possible. Currently, I'm able to get and read email from my own inbox and parse out the information that I want but I want to be able to impersonate another user. I plan on running this as a task. Below is what I currently have.
`
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
$olFolder = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolder::olFolderInBox)
$folder = $namespace
$items = $folder.Items | Where-Object {$_.UnRead -eq $true}

foreach($f in $items)
{ $reader = $null
$reader = New-Object System.IO.StringReader($f.Body)

while($true)
{
$line = $reader.ReadLine()
if($line -ne $null)
{
if($line.Contains("User:"))
{
[int]$startingIndex = $line.IndexOf(':') + 1
[int]$stringLength = $line.IndexOf('(') – $startingIndex
$user = $line.Substring($startingIndex,$stringLength).Trim()
Write-Verbose -Message "User: $user"

#Send me an email on this user
Send-SpecialEmailAboutUser -$useract $user
#set the message to be marked as read.
$f.UnRead = $false
break
}
}
}

$reader.Close()

Write-Host "Done..."
`

January 26, 2016 at 12:27 pm

Hi Tee,

To be honest, if it's just for one specific user, your best option is just for the user to give access their mailbox to you, or them to setup a rule which will carry out the specific requirements you have.

You could try and run script via an Invoke-Command impersonating the user though, but never tried this myself. I'm not certain whether it bypasses or uses the mail profile settings in place on the system.

January 26, 2016 at 12:38 pm

No, Invoke-Command doesn't load a full user profile. The Outlook COM object, which is what you're using, isn't so hot at cross-mailbox access, either. I'd probably opt for the Exchange server-side cmdlets if I _had_ to do this programmatically, but honestly, it's a read edge case in terms of automation support.

January 27, 2016 at 7:49 am

Thanks guys. I didn't think cross-mailbox access was possible.

January 28, 2016 at 6:06 pm

I usually use the EWS Api for Exchange, assuming you're using an Exchange server for your e-mail.

https://www.microsoft.com/en-us/download/details.aspx?id=42022

You can use powershell to interact with the APIs and connect to a mailbox and do pretty much whatever you want. Move items, retrieve an attachment from an e-mail with a specific subject, etc..

Brian