-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertfrom-EnPassJson.ps1
133 lines (104 loc) · 4.86 KB
/
Convertfrom-EnPassJson.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
function Convertfrom-EnPassJson {
[CmdletBinding()]
param (
[ValidateScript({
try {
[Byte]$ItemCount = (Get-Item -path $_ -ErrorAction stop).count
} #try
catch {
Throw $_
} #catch
if ($ItemCount -gt 1) {
Throw "Expected 1 item in path, found $ItemCount with Path $_"
} #if
return $True
})]
[Parameter(Mandatory = $True)]
[String]$Path
)
BEGIN {
Class KeePassStructure {
[string]$Title
[string]$Username
[string]$Password
[string]$Created
#! Yes it's a space in the parameter, KeePass expects a space for automatching
[string]${Last Modified}
[String]$URL
[string]$Notes
KeePassStructure(){}
KeePassStructure([System.Object]$InputObject) {
[System.Collections.Hashtable]$ItemHashtable = [System.Collections.Hashtable]::new()
[System.Collections.Hashtable]$UnkownFields = [System.Collections.Hashtable]::new()
[String]$ExtraNotes = 'Extra properties from EnPass import:'
foreach ($Field in $InputObject.fields) {
#* if there's already an item with a vlaue, we skip it
if (($ItemHashtable[$Field.label])) {
continue
} #if
$ItemHashtable[$Field.label] = $field.value
#* If the value is empty, at this point we continue to the next item
if ([String]::IsNullOrWhiteSpace($Field.value)) {
continue
} #if
#* Skips handled fields
if ($Field.label -in @('Password','Username','Notes','Website','URL')) {
continue
} #if
$UnkownFields[$Field.label] = $Field.value
} #foreach
$This.Title = $InputObject.title
$This.Username = $ItemHashtable['Username']
#* If username is empty, we attempt to use the Email, as there's no Email field in KeePassXC
if ([String]::IsNullOrWhiteSpace($This.Username)) {
$This.Username = $ItemHashtable['E-mail']
$UnkownFields.Remove('E-mail')
} #if
#* If the username and the email field is the same, we remove email
if ($This.Username -contains $ItemHashtable['E-mail']) {
$UnkownFields.Remove('E-mail')
} #if
$This.Password = $ItemHashtable['Password']
$This.Created = $InputObject.createdAt
$This.'Last Modified' = $InputObject.updated_at
$This.URL = $ItemHashtable['Website']
#* Some EnPass entries (In my case) had an URL field
if ([String]::IsNullOrWhiteSpace($This.URL)) {
$This.URL = $ItemHashtable['URL']
} #if
$This.Notes = $InputObject.note
#* If there's not any unknown fields at this point we continue to the next entry
if ($UnkownFields.Keys -eq 0) {
continue
} #if
#* If there's unknown fields we haven't handled earlier, we add them here to the notes property
foreach ($Key in $UnkownFields.Keys) {
$ExtraNotes += "`n$($Key): $($UnkownFields[$Key])"
} #if
#* If there's any notes, we add some newlines to our extra "fields", if not we just paste it in
if ([String]::IsNullOrWhiteSpace($This.notes)) {
$This.notes = $ExtraNotes
} #if
else {
$This.notes += "`n`n$ExtraNotes"
} #else
} #KeePassStructure([System.Object]$InputObject)
} #Class KeePassStructure
[System.Collections.Generic.List[KeePassStructure]]$Output = [System.Collections.Generic.List[KeePassStructure]]::new()
[String]$OutputPath = [System.IO.Path]::ChangeExtension($Path,'csv')
} #BEGIN
PROCESS {
try {
[System.Object]$EnPassItems = ConvertFrom-json -InputObject "$(Get-content -Path $Path -ErrorAction Stop)" -Depth 99 -ErrorAction Stop
} #try
catch {
Throw $_
} #catch
Foreach ($EnPassItem in $EnPassItems.Items) {
$Output.add(([KeePassStructure]::new($EnPassItem)))
} #foreach
} #PROCESS
END {
$Output | Export-Csv -Path $OutputPath -Delimiter ';' -ErrorAction Stop
} #END
} #function Convertfrom-EnPassJson