New-KeePassKey.ps1



if([Type]::GetType("NerdyMishka.KeePass.Search") -eq $null) {
    if($PSVersionTable.PSEdition -eq "Core") {
        Write-Error "Not supported ... yet"
    } else {
        [System.Reflection.Assembly]::LoadFile("$PSScriptRoot\bin\net452\NerdyMishka.BitsAndPieces.dll") | Write-Debug
        [System.Reflection.Assembly]::LoadFile("$PSScriptRoot\bin\net452\NerdyMishka.GoDark.dll") | Write-Debug
        [System.Reflection.Assembly]::LoadFile("$PSScriptRoot\bin\net452\NerdyMishka.KeePass.Core.dll") | Write-Debug
        [System.Reflection.Assembly]::LoadFile("$PSScriptRoot\bin\net452\NerdyMishka.KeePass.Operations.dll") | Write-Debug
    }
}

function New-KeePassKey() {
<#
    .SYNOPSIS
    Creates a new KeePass master key
 
    .DESCRIPTION
    The master key is a composite key that are required to open KeePass
    database files. Without it, the file can not be decrypted.
 
    .PARAMETER Password
    (Optional) The password for the composite key.
 
    .PARAMETER KeyFile
    (Optional) A file of bytes for the composites key.
 
    .EXAMPLE
    $key = New-KeePassKey -Passsword "your pass phrase"
 
    .EXAMPLE
    $key = New-KeePassKey -KeyFile "$home/Desktop/KP.key"
 
#>

    Param(
        [string] $Password = $null,
        [string] $KeyFile = $null ,
        [switch] $UserAccount
    )

    if([string]::IsNullOrWhiteSpace($Password)) {
        $Password = $null;
    }

    if(![string]::IsNullOrWhiteSpace($KeyFile)) {
        if(!(Test-Path $KeyFile)) {
            throw [System.ArgumentException] "Could not find KeyFile at $KeyFile";
        }

        $KeyFile = (Resolve-Path $KeyFile).Path;
    }

  
    return ,[NerdyMishka.KeePass.DbOperations]::CreateKey($Password, $KeyFile)
}