public/Import-KeePassBinary.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Import-KeePassBinary() {
    Param(
        [Parameter(ValueFromPipeline = $true)]
        [NerdyMishka.KeePass.IKeePassPackage] $Package,

        [Parameter(ValueFromPipeline = $true)]
        [NerdyMishka.KeePass.IKeePassEntry] $Entry,

     
        [string] $Path,

        [Parameter(Mandatory = $True, Position = 0)]
        [String] $Name,
        
        [Parameter(Mandatory = $true, Position = 1)]
        [Byte[]] $Data 
    )

    if($null -eq $Entry -and $null -eq $Package) {
        throw [System.ArgumentNullException] "Both Entry and Package can not be null."
    }

    if($Entry) {
        $Package = $Entry.Owner;
    } else {
        if([string]::IsNullOrWhiteSpace($Path)) {
            throw [System.ArgumentNullException] "Path"
        }

        $Entry = $Package.FindEntry($Path);
    }

   
    if($null -eq $entry) {
        Write-Warning "Could not locate $Path"
        return $null;
    }

    $Package.AttachBinary($entry, $Name, $Data)
}