New-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
#.ExternalHelp psKeePass.Help.xml
function New-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=$true, Position = 1, HelpMessage="Use path to KeePass database.")]
                [string]$KeyPassFile,
            [Parameter(Mandatory=$false,HelpMessage='Use MasterPassword to KeePass database.')]
                [Security.SecureString]$MasterPassword=(Get-KPSecurePassword -Alias Default).MasterPassword,
            [Parameter(Mandatory=$false,HelpMessage='Use to set Title the new entry.')]
                [String]$EntryTitle,
            [Parameter(Mandatory=$False,HelpMessage='Use to set Username the new entry.')]
                [String]$EntryUserName,
            [Parameter(Mandatory=$False, ParameterSetName="EntryPassword",HelpMessage='Use to set Password the new entry.')]
                [String]$EntryPassword,
            [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,HelpMessage='Use to Url the new entry.')]
                [String]$EntryUrl,
            [Parameter(Mandatory=$False,HelpMessage='Use to set Tags the new entry.')]
                [String[]]$Tags,
            [Parameter(Mandatory=$False,HelpMessage='Use to set Notes the new entry.')]
                [String]$EntryNotes,
            [Parameter(Mandatory=$False,HelpMessage='Use to set logical path to the new entry.')]
                [String]$GroupPath,
            [Parameter(Mandatory=$False,HelpMessage='Use to output the new entry.')]
                [Switch]$PassThru

    )



    BEGIN
    {
        $currentMethod = (Get-PSCallStack)[0].Command
        if (-not $MasterPassword)
        {
            do
            {
                $MasterPassword = Read-Host -Prompt "Type the Master Password to KeepPass Database `n$($KeyPassFile)" -AsSecureString
            }While(-not $MasterPassword)
        }


        if (Test-Path $KeyPassFile)
        {
            if (-not $ConnectionInfo)
            {
                $connectionInfo = New-Object KeePassLib.Serialization.IOConnectionInfo
                $connectionInfo.Path = $KeyPassFile
                New-KPParamHistory -Function $currentMethod -Parameter KeyPassFile -Content $KeyPassFile
            }
        }
        else
        {
            Write-Host File $KeyPassFile not found. -ForegroundColor Red
            break;
        }

        $kpDatabase = new-object KeePassLib.PwDatabase

        $compositeKey = new-object KeePassLib.Keys.CompositeKey
        #$m_pKey.AddUserKey((New-Object KeePassLib.Keys.KcpUserAccount))
        $compositeKey.AddUserKey((New-Object KeePassLib.Keys.KcpPassword($MasterPassword | ConvertTo-KPPlainText)));
        $StatusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger

        try
        {
            $kpDatabase.Open($connectionInfo,$compositeKey,$StatusLogger)
        }
        catch [KeePassLib.Keys.InvalidCompositeKeyException]
        {
            Write-Host Incorrect password. $($_.Exception.Message) -ForegroundColor Red
            break;
        }
        catch [Exception]
        {
            Write-Host $_.Exception.Message
            Write-KPLog -message $_ -Level EXCEPTION
            break;
        }

        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
    {
        try
        {
            $rootGroup = $kpDatabase.RootGroup
            $PwGroup = Get-PwGroup $rootGroup $GroupPath


            # The $True arguments allow new UUID and timestamps to be created automatically:
            $PwEntry = New-Object -TypeName KeePassLib.PwEntry( $PwGroup[0], $True, $True )
 

            if (-not $EntryPassword)
            {
                $EntryPassword = Get-KPRandomPassword -Length $EntryPasswordLenght -Complex:$EntryPasswordComplex.IsPresent
            }

            $EntryNotes += "`n#New entry by psKeePass"
    
            # Protected strings are encrypted in memory:
            $pTitle = New-Object KeePassLib.Security.ProtectedString($True, $EntryTitle)
            $pUser = New-Object KeePassLib.Security.ProtectedString($True, $EntryUserName)
            $pPW = New-Object KeePassLib.Security.ProtectedString($True, $EntryPassword)
            $pURL = New-Object KeePassLib.Security.ProtectedString($True, $EntryUrl)
            $pNotes = New-Object KeePassLib.Security.ProtectedString($True, $EntryNotes)
 
            $PwEntry.Strings.Set("Title", $pTitle)
            $PwEntry.Strings.Set("UserName", $pUser)
            $PwEntry.Strings.Set("Password", $pPW)
            $PwEntry.Strings.Set("URL", $pURL)
            $PwEntry.Strings.Set("Notes", $pNotes)
        
            $Tags | % { $null = $PwEntry.AddTag($_) }

            $PwGroup[0].AddEntry($PwEntry, $True)
 
            # Notice that the database is automatically saved here!
            #$StatusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger
            $kpDatabase.Save($StatusLogger)


            if ($PassThru.IsPresent)
            {
                Get-KPEntry -ConnectionInfo $connectionInfo -CompositeKey $compositeKey -Uuid $PwEntry.Uuid
            }
        }
        catch [Exception]
        {
            Write-Host $_.Exception.Message
            Write-KPLog -message $_ -Level EXCEPTION
        }

 
    }
    END
    {
        if ($kpDatabase.IsOpen)
        {
            $kpDatabase.Close()
        }
    }
}