Set-KPEntry.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#.ExternalHelp psKeePass.Help.xml
function Set-KPEntry
{
    # http://technet.microsoft.com/en-us/library/hh847872.aspx
     [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  SupportsShouldProcess=$true, 
                  PositionalBinding=$false
                  #HelpUri = 'http://www.microsoft.com/',
                  #ConfirmImpact='Medium'
                  )]
     #[OutputType([String])]

    param(
            [Parameter(Mandatory=$false,ValueFromPipeline=$True,DontShow)]
                [KeePassLib.PwEntry]$InputObject,
            [Parameter(Mandatory=$False)]
                [String]$EntryTitle,
            [Parameter(Mandatory=$False)]
                [String]$EntryUserName,
            [Parameter(Mandatory=$False, ParameterSetName="EntryPassword")]
                [Security.SecureString]$EntryPassword,
            [Parameter(Mandatory=$False, ParameterSetName="AutoPassword",HelpMessage='Use to define the password length.')]
                [Switch]$AutoResetPassword,
            [Parameter(Mandatory=$False, ParameterSetName="AutoPassword",HelpMessage='Use to define the password length.')]
                [int]$EntryPasswordLenght=20,
            [Parameter(Mandatory=$False, ParameterSetName="AutoPassword",HelpMessage='Use to generate password.')]
                [Switch]$EntryPasswordComplex,
            [Parameter(Mandatory=$False)]
                [String]$EntryUrl,
            [Parameter(Mandatory=$False,HelpMessage='Use to Add new Tags.')]
                [String[]]$AddTags,
            [Parameter(Mandatory=$False,HelpMessage='Use to Remove Tags.')]
                [String[]]$RemoveTags,
            [Parameter(Mandatory=$False)]
                [String]$EntryNotes,
            [Parameter(Mandatory=$False,HelpMessage='Use to move entry to group.')]
                [String]$GroupPath,
            [Parameter(Mandatory=$False)]
                [Switch]$PassThru

    )

    BEGIN
    {
        function Get-PwGroup ($RootGroup,[String]$GroupPath)
        {
            $Tree = $GroupPath -split '\\'
            if ($Tree)
            {
                #$pwGroup = $RootGroup.Groups
                $pwGroup = $RootGroup
                foreach ($groupName in $Tree)
                {
                            if (-not [String]::IsNullOrEmpty($groupName))
                            {
                                if ($groupName -in $pwGroup.Name)
                                {
                                    $pwGroup = $pwGroup | ? {$_.Name -eq $groupName}
                                    $out = $pwGroup
                                    $pwGroup = $pwGroup.Groups
                                    $isValidGroup = $True
                                }
                                else
                                {
                                    $isValidGroup = $False
                                    break
                                }
                            }
                        }
                if ($isValidGroup)
                {
                    #return [Array]$Tree | ? { -not [String]::IsNullOrEmpty($_) }
                    return $out
                }
                else
                {
                    return $null
                }
            }
            else
            {
                return $RootGroup
            }
        }
    }
    PROCESS
    {
        if ($InputObject)
        {
            $InputObject | % {
                try
                {
                    if ($_ -is [KeePassLib.PwEntry] )
                    {
                        $PwEntry = $_
                        $connectionInfo = $PwEntry.connectionInfo
                        $compositeKey = $PwEntry.compositeKey

                        #$kpDatabase = $PwEntry.kpDatabase
                        $kpDatabase = new-object KeePassLib.PwDatabase
                        $statusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger

                        $kpDatabase.open($connectionInfo,$compositeKey,$statusLogger)
                        $PwEntry = $kpDatabase.RootGroup.FindEntry($PwEntry.Uuid,$true)
                    }
                    else
                    {
                        Write-Host InputObjec is not KeePassLib.PwEntry. -ForegroundColor Red
                        break
                    }

                    # Protected strings are encrypted in memory:
                    if ( $EntryTitle -and ($PwEntry.Strings.ReadSafe('Title') -ne $EntryTitle) )
                    {
                        $pTitle = New-Object KeePassLib.Security.ProtectedString($True, $EntryTitle)
                        $PwEntry.Strings.Set("Title", $pTitle)
                    }
                    if ( $EntryUserName -and ($PwEntry.Strings.ReadSafe('UserName') -ne $EntryUserName) )
                    {
                        $pUser = New-Object KeePassLib.Security.ProtectedString($True, $EntryUserName)
                        $PwEntry.Strings.Set("UserName", $pUser)
                    }

                    if ($AutoResetPassword.IsPresent)
                    {
                        $pPW = New-Object KeePassLib.Security.ProtectedString($True, (Get-KPRandomPassword -Length $EntryPasswordLenght -Complex:$EntryPasswordComplex.IsPresent ))
                        $PwEntry.Strings.Set("Password", $pPW)
                    }

                    if ( $EntryPassword -and ($PwEntry.Strings.ReadSafe('Password') -ne (ConvertTo-PlainText -SecureString $EntryPassword)) )
                    {
                        $pPW = New-Object KeePassLib.Security.ProtectedString($True, (ConvertTo-PlainText -SecureString $EntryPassword))
                        $PwEntry.Strings.Set("Password", $pPW)
                    }

                    if ( $EntryUrl -and ($PwEntry.Strings.ReadSafe('URL') -ne $EntryUrl) )
                    {
                        $pURL = New-Object KeePassLib.Security.ProtectedString($True, $EntryUrl)
                        $PwEntry.Strings.Set("URL", $pURL)
                    }
                    if ( $EntryNotes -and ($PwEntry.Strings.ReadSafe('Notes') -ne $EntryNotes) )
                    {
                        $pNotes = New-Object KeePassLib.Security.ProtectedString($True, $EntryNotes)
                        $PwEntry.Strings.Set("Notes", $pNotes)
                    }


                    if ($AddTags)
                    {
                        $AddTags | % { 
                            if ($PwEntry.Tags -notcontains $_)
                            {
                                $null = $PwEntry.AddTag($_) 
                            }
                        }
                    }

                    if ($RemoveTags)
                    {
                        $RemoveTags | % { 
                            if ($PwEntry.Tags -contains $_)
                            {
                                $null = $PwEntry.RemoveTag($_) 
                            }
                        }

                    }

                    #if ( $GroupPath -and ($PwEntry.Strings.ReadSafe('Notes') -ne $GroupPath) )
                    if ( $GroupPath -and ($GroupPath -ne $PwEntry.GroupPath) )
                    {
                        try
                        {
                            $rootGroup = $kpDatabase.RootGroup
                            $oldPwGroup = $PwEntry.ParentGroup
                            $PwGroup = Get-PwGroup $rootGroup $GroupPath 
                            if ($PwGroup)
                            {
                                $null = $kpDatabase.MergeIn($kpDatabase,[KeePassLib.PwMergeMethod]::Synchronize,$statusLogger)
                                $null = $PwGroup[0].AddEntry($PwEntry, $True, $True)
                                $null = $oldPwGroup.Entries.Remove($PwEntry)
                            }
                            else
                            {
                                Write-Host "Group Path $($GroupPath) is invalid." -ForegroundColor Cyan
                            }
                        }
                        catch [Exception]
                        {
                            Write-KPLog -message $_ -Level EXCEPTION
                            Write-Host $($_.Exception.Message) -ForegroundColor Red
                        }
                    }

                    if ($kpDatabase.IsOpen)
                    {
                        $null = $kpDatabase.MergeIn($kpDatabase,[KeePassLib.PwMergeMethod]::Synchronize,$statusLogger)
                        $null = $PwEntry.Touch($true,$true)
                        $kpDatabase.Save($statusLogger)
                        $kpDatabase.Close()
                    }

                    if ($PassThru.IsPresent)
                    {
                        Get-KPEntry -ConnectionInfo $connectionInfo -CompositeKey $compositeKey -Uuid $PwEntry.Uuid
                    }

                }
                catch [Exception]
                {
                    Write-KPLog -message $_ -Level EXCEPTION
                    Write-Host $($_.Exception.Message) -ForegroundColor Red
                    continue
                }

            }#InputObject
        }
    }
    END
    {
        
    }
}