public/Save-KeePassPackage.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
function Save-KeePassPackage() {
    Param(
        [Parameter(ValueFromPipeline = $true)]
        [NerdyMishka.KeePass.KeePassPackage] $Package,
        
        [NerdyMishka.KeePass.MasterKey] $Key,

        [Parameter(Position = 0)]
        [String] $Path,

        [Switch] $Force 
    )

    if(!$Force.ToBool() -and (Test-Path $Path)) {
        Write-Warning "File already exists $Path";
        return null; 
    }

    $directoryName = [IO.Path]::GetDirectoryName($Path);
    
    if(!(Test-Path $directoryName)) {
        New-Item $directoryName -ItemType Directory -Force
    }

    

    [NerdyMishka.KeePass.IKeePassPackageSerializer]$serializer = (New-Object NerdyMishka.KeePass.Xml.KeePassPackageXmlSerializer)

    if($Key -eq $null) {
        $Key = $Package.MasterKey;
    }
    $fs = [IO.File]::OpenWrite($Path);
    $result = $Package.Save($key, $fs, $serializer);
    $fs.Dispose()

    return $result;
}