Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
get-calvus opened this issue Feb 17, 2023 · 0 comments

Comments

@get-calvus
Copy link

get-calvus commented Feb 17, 2023

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant