1
Vote

add user

description

$PathToKeePassFolder = "C:\Program Files (x86)\KeePass Password Safe 2"

(Get-ChildItem -recurse $PathToKeePassFolder|Where-Object {($_.Extension -EQ ".dll") -or ($_.Extension -eq ".exe")} | ForEach-Object { $AssemblyName=$_.FullName; Try {[Reflection.Assembly]::LoadFile($AssemblyName) } Catch{ }} ) | out-null
[String]$PasswordToDB = "******"
$PathToDB = "C:\BASE.kdbx"
function Get-UserPass
{
[CmdletBinding()]
[OutputType([String[]])]
Param
(
    # Path To password DB
   [string]$EntryToFind,
    $PathToDB = "C:\BASE.kdbx"
    # Entry to find in DB

   # Password used to open KeePass DB        
    #[Parameter][String]$PasswordToDB
)

$PwDatabase = new-object KeePassLib.PwDatabase

$m_pKey = new-object KeePassLib.Keys.CompositeKey
$m_pKey.AddUserKey((New-Object KeePassLib.Keys.KcpPassword($PasswordToDB)));

$m_ioInfo = New-Object KeePassLib.Serialization.IOConnectionInfo
$m_ioInfo.Path = $PathToDB

$IStatusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger

$PwDatabase.Open($m_ioInfo,$m_pKey,$IStatusLogger)


$pwItems = $PwDatabase.RootGroup.GetObjects($true, $true)

foreach($pwItem in $pwItems)
{

    [string]$Title =  $pwItem.Strings.ReadSafe("Title")
    #$pwItem.Strings.ReadSafe("UserName")

    if ($EntryToFind -eq $pwItem.Strings.ReadSafe("UserName") )
    {write-host $Title _____  $pwItem.Strings.ReadSafe("UserName")  ______  $pwItem.Strings.ReadSafe("Password")}
}
$PwDatabase.Close()
# $PasswordToDB = $null
}

function add-UserPass
{
[CmdletBinding()]
[OutputType([String[]])]
Param
(
   [string]$Name,
   [string]$password,
   [string]$UserName
   )
   $PwDatabase = new-object KeePassLib.PwDatabase

$m_pKey = new-object KeePassLib.Keys.CompositeKey
$m_pKey.AddUserKey((New-Object KeePassLib.Keys.KcpPassword($PasswordToDB)));

$m_ioInfo = New-Object KeePassLib.Serialization.IOConnectionInfo
$m_ioInfo.Path = $PathToDB

$IStatusLogger = New-Object KeePassLib.Interfaces.NullStatusLogger

$PwDatabase.Open($m_ioInfo,$m_pKey,$IStatusLogger)


$pwItems = $PwDatabase.RootGroup.GetObjects($true, $true)
$general = $PwDatabase.RootGroup.FindGroup($PwDatabase.RootGroup.Groups.Uuid[0].UuidBytes,0)

$ent = New-Object KeePassLib.PwEntry($general , $true , $true)

$title = New-Object KeePassLib.Security.ProtectedString($true , "$Name")
$pass = New-Object KeePassLib.Security.ProtectedString($true , "$password")
$user = New-Object KeePassLib.Security.ProtectedString($true , "$UserName")

$ent.Strings.Set("Title",$title)
$ent.Strings.Set("Password",$pass)
$ent.Strings.Set("UserName",$user)

$general.AddEntry($ent,1)
$PwDatabase.Save($IStatusLogger)
}

comments