PSKeePass / PoShKeePass

PowerShell module for KeePass
MIT License
255 stars 56 forks source link

Unable to create new entry - InvalidDatabaseConnectionException : The database is not open. #209

Open get-calvus opened 1 year ago

get-calvus commented 1 year ago

Ok, been beating my head on this a while and can't find anything helpful...

I'm writing a script that creates an Active Directory user. Inside this script I have a function which uses PoShKeePass to connect to our KeePass database and write the required username to a new entry. I've ironed out all of the kinks except for the actual creation of the new entry. I've narrowed it down to possibly being an issue with the requirement of the password being encrypted when using New-KeePassEntry, although it's very possible that I'm wrong. Below are the pertinent parts of the function. I can give the entire function if needed.

Here's the error: TerminatingError(New-Object): "Exception calling ".ctor" with "1" argument(s): "Could not load type 'System.Security.Cryptography.ProtectedMemory' from assembly 'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=RemovedForSafety'.""

Everything runs great until it gets to the New-KeePassEntry line. Oh, and I realize the passwords aren't secure. It's a temporary password. As for the database password, that's a prompt in my full script. I got tired of continually typing it.

Thanks in advance for any help you can provide.

EDIT: I cannot get the code below to show up as code. Not sure what's going on there.

#Database variables $DBPath = "\\Path\To\Keepass Database" $DBName = "Test" $DBGroup = 'Test/Windows $KPDriveName = "KPDrive" $DBFile = $KPDriveName + ":\" + "Test.kdbx"

# User variables $new_first = "My" $new_last = "Lastname" $Name = $new_first + ' ' + $new_last $new_firstLower = "$new_first".ToLower() $Initial = $new_firstLower.Substring(0, 1) $new_lastLower = "$new_last".ToLower() $SAM = $Initial + $new_lastLower

# Password variables $Date = Get-Date -Format "MMddyyyy" $TempPass = "$DateStuff!$" $UserPassSecure = $(ConvertTo-SecureString -String $TempPass -AsPlainText -Force) $KPPass = $(ConvertTo-SecureString -String "SuperDooperSecret" -AsPlainText -Force)`

# KeePass user entry splat $KPSplat = @{ DatabaseProfileName = $DBName KeePassEntryGroupPath = $DBGroup Title = $Name UserName = $SAM KeePassPassword = $UserPassSecure }

If ((Get-KeePassDatabaseConfiguration).DatabasePath -eq $DBFile) { Write-Host "Already connected to existing KeePass database." } Else { # Create KeePass DB config New-KeePassDatabaseConfiguration -DatabasePath $DBFile -DatabaseProfileName $DBName -UseMasterKey -Verbose -ErrorAction Stop Write-Host "Connected to KeePass database...rn" Write-Host "rn" }

Try { New-KeePassEntry @KPSplat -MasterKey $KPPass -Verbose Write-Host "Entry added for $Name to KeePass.rn" Write-Host "rn" } Catch { Write-Host "[ERROR]t Could not add entry to KeePass: $($_.Exception.Message)rn" Write-Host "Please add entry to KeePass manually.nTitle: $NamenUsername: $SAMnPassword: "$DatePower!$"rn" Write-Host "rn" }