forked from jasonadsit/powershell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConvertFrom-Rtf.ps1
197 lines (144 loc) · 6.19 KB
/
ConvertFrom-Rtf.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#Requires -Version 3.0
function ConvertFrom-Rtf {
<#
.SYNOPSIS
Converts richtext (rtf) documents to plaintext
.DESCRIPTION
Converts richtext (rtf) documents to plaintext and outputs
text strings or psobjects based on a switch.
.EXAMPLE
Get-ChildItem 'C:\Windows' -Recurse `
-File `
-Filter *.rtf `
-ErrorAction SilentlyContinue |
Select-Object -First 5 |
ConvertFrom-Rtf
Converts first five rtf files in C:\Windows to plaintext
.INPUTS
System.IO.FileInfo
.OUTPUTS
System.Management.Automation.PSCustomObject
.PARAMETER PathToFile
System.IO.FileInfo objects to operate on
.PARAMETER AsObject
Switch to output psobjects instead of strings
.PARAMETER Hash
Switch to hash files or not
.PARAMETER epoch
DateTime of epoch (01/01/1970)
.NOTES
#######################################################################################
Author: @oregon-national-guard/systems-administration
Version: 1.0
#######################################################################################
License: https://github.com/oregon-national-guard/HunterGatherer/blob/master/LICENCE
#######################################################################################
.LINK
https://github.com/oregon-national-guard
.LINK
https://creativecommons.org/publicdomain/zero/1.0/
.LINK
http://blog.kmsigma.com/2014/10/01/converting-rtf-to-txt-via-powershell/
#>
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath','FullName')]
[System.IO.FileInfo[]]
$PathToFile = @((Get-Item 'C:\Windows\Help\en-US\credits.rtf')),
[switch]
$AsObject,
[switch]
$Hash,
[datetime]
$epoch = '01/01/1970'
) #param
begin {
$StartTime = (Get-Date)
$NameOfFunction = 'ConvertFrom-Rtf'
$CountFiles = ($PathToFile | Measure-Object).Count
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.Windows.Forms
#
# Thank @kmsigma for the rtf conversion part.
# I just adapted it for the pipeline and put some
# window dressing on it to fit my use case.
#
} #begin
process {
$PathToFile | ForEach-Object {
$RichTextBox = New-Object -TypeName System.Windows.Forms.RichTextBox
try {
$RichTextBox.Rtf = [System.IO.File]::ReadAllText($_.FullName)
} catch {
Remove-Variable RichTextBox -ErrorAction SilentlyContinue
$ExceptionName = $_.Exception.GetType().FullName
$RichTextBox = New-Object -TypeName psobject -Property @{
"Text" = "#!#!# Failed to convert with a $ExceptionName exception #!#!#"
}
} finally {
if ($AsObject) {
if ($Hash) {
New-Object -TypeName psobject -Property @{
"Name" = $_.Name
"FullName" = $_.FullName
"Text" = $RichTextBox.Text
"LastWriteTime" = $(($_.LastWriteTime) -as [datetime])
"EpochTime" = $(
(New-TimeSpan -Start $epoch -End (
[system.timezoneinfo]::ConvertTime(
($_.LastWriteTime),([system.timezoneinfo]::UTC)
))
).TotalSeconds
)
"TimeStamp" = $((($_.LastWriteTime).GetDateTimeFormats('s')) -as [string])
"Length" = $_.Length
"SHA256" = $((Get-FileHash -Path $_.FullName -Algorithm SHA256).Hash)
} |
Select-Object -Property LastWriteTime,
TimeStamp,
EpochTime,
Name,
FullName,
Length,
SHA256,
Text
Remove-Variable RichTextBox -ErrorAction SilentlyContinue
} elseif (!($Hash)) {
New-Object -TypeName psobject -Property @{
"Name" = $_.Name
"FullName" = $_.FullName
"Text" = $RichTextBox.Text
"LastWriteTime" = $(($_.LastWriteTime) -as [datetime])
"EpochTime" = $(
(New-TimeSpan -Start $epoch -End (
[system.timezoneinfo]::ConvertTime(
($_.LastWriteTime),([system.timezoneinfo]::UTC)
))
).TotalSeconds
)
"TimeStamp" = $((($_.LastWriteTime).GetDateTimeFormats('s')) -as [string])
"Length" = $_.Length
} |
Select-Object -Property LastWriteTime,
TimeStamp,
EpochTime,
Name,
FullName,
Length,
Text
Remove-Variable RichTextBox -ErrorAction SilentlyContinue
} #if ($Hash)
} elseif (!($AsObject)) {
$RichTextBox.Text
Remove-Variable RichTextBox -ErrorAction SilentlyContinue
} # if else (`$AsObject)
} #try catch finally
} #ForEach
} #process
end {
$EndTime = (Get-Date)
Write-Verbose "Finished running $NameOfFunction on $CountFiles files"
Write-Verbose "Elapsed Time: $(($EndTime - $StartTime).TotalSeconds) seconds"
} #end
} #function ConvertFrom-Rtf