Created
October 29, 2014 14:57
-
-
Save marc-fez/9591722e384f16376926 to your computer and use it in GitHub Desktop.
KPScript retrieve password in Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-KeepassPW{ | |
$result = $null | |
$KeePath = "C:\Program Files (x86)\KeePass Password Safe 2\KPScript.exe" | |
# Path to your database | |
$KeeDataPath = "C:\Users\MyUser\Documents\KeePass\Database.kdbx" | |
while(!$result){ | |
$pass = Read-Host 'What is your KeePassword?' -AsSecureString | |
$KePassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) | |
$KeeCommands = @( | |
"-c:GetEntryString" | |
"""$KeeDataPath""" | |
"-pw:$KePassword" | |
"-Field:Password" | |
"-refx-UUID:B6881874DF188E4CB4201033EE2AAE51" # Best way I coud find to locate a KP entry, check properties of your entry | |
"-FailIfNotExists" | |
) | |
$result = & $KeePath $KeeCommands # $result will be an array, index[0] will either be your password or an error string | |
if(!$result -or $result[0] -match "^E: The composite key is invalid!$"){ | |
$result = $null | |
write-host "Invalid Password or error retrieving password. Please try again." | |
} | |
} | |
return $result[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
KPScript requires quotes around passwords with spaces, so you could use this:
"-pw:`"$KePassword`""