Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Last active October 20, 2019 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janikvonrotz/898be0891058ee091dae8c68a8e2b91e to your computer and use it in GitHub Desktop.
Save janikvonrotz/898be0891058ee091dae8c68a8e2b91e to your computer and use it in GitHub Desktop.
Convert KeePass to Pass #KeePass #PowerShell
[xml]$Content = Get-Content -Path "KeepassData.xml"
<#
Access the xml data:
Content.KeePassFile.Root.Group.Group.Entry[67].String[0].Value
#>
function Traverse-Tree ($Node, $ParentPath) {
$Path = $ParentPath + "/" + $Node.Name
$ChildNodes = $Node.Group
$Entries = $Node.Entry | Where-Object { $_.Times.Expires -eq "False" }
if($Entries -and ($Node.Name -notcontains "Recycle Bin")) {
foreach ($Entry in $Entries) {
$Content = ""
$Title = ""
$Password = ""
foreach ($Field in $Entry.String) {
switch($Field.Key) {
"Title" { $Title = $Field.Value }
"Password" { $Password = $Field.Value.'#text' }
"Notes" {
$Content += "$($Field.Key): $(($Field.Value -replace "\n","\n" ) -replace ":", "=")`n"
Write-Host $Content
}
default {
$Content += "$($Field.Key): $($Field.Value -replace "\n","\n" )`n"
}
}
}
@{
Path = ($Path + "/" + $Title) -replace " ", "_"
Content = $Password + "`n" + $Content
}
}
}
if($ChildNodes) {
foreach ($ChildNode in $ChildNodes ) {
Traverse-Tree $ChildNode $Path
}
}
}
$Content.KeePassFile.Root.Group.Group | ForEach-Object {
Traverse-Tree -Node $_ -ParentPath ""
} | ForEach-Object {
Write-Host "Creating pass entry: $($_.Path)"
Write-Host $_.Content
$_.Content | & pass insert -m $_.Path
}
[xml]$Content = Get-Content -Path "KeepassData.xml"
$Entries = $Content.KeePassFile.Root.Group.Group | Where-Object { $_.Times.Expires -eq "False" -and $_.Name -eq "Private" } | %{$_.Entry}
$Entries | % {
$String = $_.String | where{$_.Value -eq "GitHub"}
if($String) {
$_.String | %{
if($_.Key -eq "2FA App Passcode") {
$_.Value -replace "\n","\n"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment