forked from iricigor/OutlookConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport-OutlookMessageBody.ps1
141 lines (104 loc) · 5.64 KB
/
Export-OutlookMessageBody.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function Export-OutlookMessageBody {
<#
.SYNOPSIS
OutlookConnector function: Saves message body of Outlook message to a file on disk.
.DESCRIPTION
Saves body text of one or messages to a file on disk at specified path. All messages are saved in same folder, and file names are built based on customizable parameter FileNameFormat.
Messages can be obtained by one of Get-Outlook commands. Messages are saved in HTML, TXT or RTF format. If message with same name exists, numbering will be added at the end of file name.
Message body does not contain header information, i.e. info who sent message, when, etc. Also, it has no attachments.
.Example
(Get-OutlookInbox)[0] | Export-OutlookMessageBody -OutputFolder 'C:\tmp' -ExportFormat HTML
Saves body of first message in Inbox as HTML file
.EXAMPLE
Get-OutlookMessage -DefaultFolder DeletedItems | Export-OutlookMessageBody -OutputFolder 'C:\tmp\deleted' -ExportFormat TXT
Saves all messaged from Deleted items Outlook folder to a txt files on a disk
.PARAMETER Messages
Mandatory parameter which is content of one or more messages that will be saved to disk.
Messages can be obtained with one of Get-Outlook commands. Type Get-Command Get-Outlook* for list of commands.
Messages can be provided either as parameter, or via pipeline.
.PARAMETER OutputFolder
Mandatory parameter which specifies to which folder messages will be saved. It can be both local disk, as well as network location.
.PARAMETER FileNameFormat
Optional parameter that specifies how individual files will be named based. If omitted, files will be saved in format 'FROM= %SenderName% SUBJECT= %Subject%'.
File name can contain any of message parameters surrounded with %. For list of parameters, type Get-OutlookInbox | Get-Member.
Custom format can be specified after a | character within the %, e.g. %ReceivedTime|yyyyMMddhhmmss%.
.PARAMETER ExportFormat
Mandatory parameter which specifies to which format message body will be exported to. Allowed values are HTML, TXT (text) and RTF (rich-text).
.PARAMETER SkippedMessages
Optional parameter that specifies varaible to which will be stored messages that can not be processed.
Messages can be skipped for different reasons (wrong object, missing property specified in FileNameFormat parameter, etc.
Variable must be referenced, i.e. sent in format [ref]$Variable, and it must be declared in advance. Current value of variable will be deleted.
.OUTPUTS
Function is not returning any value.
.LINK
about_OutlookConnector
.NOTES
NAME: Export-OutlookMessageBody
AUTHOR: Igor Iric, [email protected]
CREATEDATE: November, 2015
#>
# ---------------------- [Parameters definitions] ------------------------
[cmdletbinding()]
Param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidateNotNullOrEmpty()] [psobject[]]$Messages,
[parameter(Mandatory=$true,ValueFromPipeline=$false)] [string]$OutputFolder,
[parameter(Mandatory=$false,ValueFromPipeline=$false)] [string]$FileNameFormat='FROM= %SenderName% SUBJECT= %Subject%',
[parameter(Mandatory=$true,ValueFromPipeline=$false)] [ValidateSet('HTML','TXT','RTF')] [string]$ExportFormat,
[parameter(Mandatory=$false,ValueFromPipeline=$false)] [ref]$SkippedMessages
) #end param
# ------------------------- [Function start] -----------------------------
BEGIN {
Write-Verbose -Message 'Export-OutlookMessageBody starting...'
# convert format message to real file name, replace %...% with message attribute
$ReqProps = @('Subject','HTMLBody','RTFBody','Body')
$ReqProps += Get-Properties($FileNameFormat)
# resolve relative path since MailItem.SaveAs does not support them
$OutputFolderPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputFolder)
# initialize queue for skipped messages, if it is passed
if ($SkippedMessages) {
$SkippedMessages.Value = @()
}
} # End of BEGIN block
PROCESS {
foreach ($Message in @($Messages)) {
# check input object
try {
Validate-Properties -InputObject $Message -RequiredProperties $ReqProps
} catch {
if ($SkippedMessages) {
$SkippedMessages.Value += $Message # adding skipped messages to referenced variable if passed
}
Write-Error $_
Continue # next foreach
}
Write-Verbose -Message ('Processing '+($Message.Subject))
# generate file name
$FileName = Expand-Properties -InputObject $Message -Pattern $FileNameFormat
$FileName = Get-ValidFileName -FileName $FileName
try {
$FullFilePath = Get-UniqueFilePath -FolderPath $OutputFolderPath -FileName $FileName -Extension $ExportFormat
} catch {
Write-Error $_
Continue # next foreach
}
# save message to disk
Write-Verbose -Message "Saving message body to $FullFilePath"
try {
switch ($ExportFormat) {
'HTML' {Set-Content -Value ($Message.HTMLBody) -LiteralPath $FullFilePath}
'RTF' {Set-Content -Value ($Message.RTFBody) -LiteralPath $FullFilePath -Encoding Byte}
'TXT' {Set-Content -Value ($Message.Body) -LiteralPath $FullFilePath}
}
} catch {
if ($SkippedMessages) {
$SkippedMessages.Value += $Message # adding skipped messages to referenced variable if passed
}
Write-Error -Message ('Message save exception. '+$Error[0].Exception)
}
} # End of foreach
} # End of PROCESS block
END {
Write-Verbose -Message 'Export-OutlookMessageBody completed.'
} # End of END block
# ------------------------- [End of function] ----------------------------
}