Created
April 6, 2018 18:29
-
-
Save JohnLBevan/6ff416b0da66215b6a795862c94d2e1c to your computer and use it in GitHub Desktop.
Find everyone a mail's addressed to by loading an MSG file from MS Outlook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-MailRecipients { | |
| [CmdletBinding(DefaultParameterSetName = 'ByFilename')] | |
| Param ( | |
| [Parameter(Mandatory = $true, ParameterSetName = 'ByFilename', ValueFromPipeline = $true)] | |
| [string]$Path | |
| , | |
| [Parameter(Mandatory = $false, ParameterSetName = 'ByFilename')] | |
| [PSObject]$Outlook = $null | |
| , | |
| [Parameter(Mandatory = $true, ParameterSetName = 'ByMessage')] | |
| [PSObject]$Message | |
| ) | |
| Begin { | |
| $closeOutlook = $false #only close outlook if we openned it | |
| if (($PSCmdlet.ParameterSetName -eq 'ByFilename') -and ($Outlook -eq $null)) { | |
| $Outlook = (New-Object -ComObject 'Outlook.Application') | |
| $closeOutlook = $true | |
| } | |
| } | |
| Process { | |
| if ($PSCmdlet.ParameterSetName -eq 'ByFilename') { | |
| $Message = $Outlook.Session.OpenSharedItem($Path) | |
| } | |
| $Message.Recipients | ForEach-Object { | |
| $_.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E") #see https://stackoverflow.com/a/45296809/361842 for mapping between properties & their schemas | |
| } | |
| } | |
| End { | |
| if ($closeOutlook) { | |
| $Outlook.Quit() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment