<# FPS:CONTAINERS:ALOHA-P1:DATARESOURCES:SC_NEWKPENTRY:ACTIVE #>

 

if (!(Push-NeededFor NewKPEntry)) {

    # Remove a prior patch if it already exists

    Remove-Item -Path Function:\NewKPEntry

} else {

    # Only load the help line if a prior release is not present, otherwise just keep whats there.

    $kpShortcuts = @($kpShortcuts

    (ConvertTo-DotFill $global:SCDotLen 'NewKPEntry')+"[-KPGroupPath [Path], -Title [Title], -UID [UID], -EntryPwd [PWD] -AsPSCredential (o) -AsPlainText (o) ] "

    (ConvertTo-DotFill $global:SCDotLen '')+"Function to create a new KeePass Database Entry."

    )

}

 

function NewKPEntry

{

    <#

 

        .DESCRIPTION

            This function allows for the creation of KeePass Database Entries with basic properites available for specification.

        .PARAMETER KPEntryGroupPath

            Specify this parameter if you wish to only return entries form a specific folder path.

            Notes:

                * Path Separator is the foward slash character '/'

        .PARAMETER Title

            Specify the Title of the new KeePass Database Entry.

        .PARAMETER UID

            Specify the UID of the new KeePass Database Entry.

        .PARAMETER KPPwd

            *Specify the KPPwd of the new KeePass Database Entry.

            *Notes:

                *This Must be of the type SecureString or KeePassLib.Security.ProtectedString

        .PARAMETER Notes

            Specify the Notes of the new KeePass Database Entry.

        .PARAMETER URL

            Specify the URL of the new KeePass Database Entry.

        .PARAMETER PassThru

            Specify to return the newly created keepass database entry.

        .PARAMETER IconName

            Specify the Name of the Icon for the Entry to display in the KeePass UI.

        .PARAMETER Expires

            Specify if you want the KeePass Object to Expire, default is to not expire.

        .PARAMETER ExpiryTime

            Datetime expiration Time value.

        .EXAMPLE

            PS> NewKPEntry -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title' -UID 'Domain\svcAccount' -EntryPwd $(NewKPPwd -upper -lower -digits -length 20)

 

            This example creates a new keepass database entry in the General/TestAccounts database group, with the specified Title and UID. Also the function New-KPPwd is used to generated a random password with the specified options.

        .EXAMPLE

            PS> NewKPEntry -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title' -UID 'Domain\svcAccount' -EntryPwd $(NewKPPwd -PasswordProfileName 'Default' )

 

            This example creates a new keepass database entry in the General/TestAccounts database group, with the specified Title and UID. Also the function New-KPPwd with a password profile specifed to create a new password genereated from options saved to a profile.

        .EXAMPLE

            PS> NewKPEntry  -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title' -UID 'Domain\svcAccount' -EntryPwd ($(ConvertTo-SecureString -String 'apassword' -AsPlainText -Force))

 

            This example creates a new keepass database entry with the specified Title, UID and manually specified password converted to a securestring.

        .INPUTS

            String

            SecureString

        .OUTPUTS

            $null

    #>

    [CmdletBinding()]

    param

    (

        [Parameter(Position = 0, Mandatory, ValueFromPipelineByPropertyName)]

        [ValidateNotNullOrEmpty()]

        [Alias('FullPath')]

        [String] $KPEntryGroupPath,

 

        [Parameter(Position = 1)]

        [ValidateNotNullOrEmpty()]

        [String] $Title,

 

        [Parameter(Position = 2)]

        [ValidateNotNullOrEmpty()]

        [String] $UID,

 

        [Parameter(Position = 3)]

        [ValidateNotNullOrEmpty()]

        [ValidateScript({$_.GetType().Name -eq 'ProtectedString' -or $_.GetType().Name -eq 'SecureString'})]

        [PSObject] $EntryPwd,

 

        [Parameter(Position = 4)]

        [ValidateNotNullOrEmpty()]

        [String] $Notes,

 

        [Parameter(Position = 5)]

        [ValidateNotNullOrEmpty()]

        [String] $URL,

 

        [Parameter(Position = 6)]

        [ValidateNotNullOrEmpty()]

        [string] $IconName = 'Key',

 

        [Parameter(Position = 7)]

        [switch] $Expires,

 

        [Parameter(Position = 8)]

        [DateTime] $ExpiryTime,

 

        [Parameter(Position = 9)]

        [Switch] $PassThru

 

    )

    begin

    {

 

        $overrideSrc, $overrideSnippet = '','' # Initialize override variables

        #$overrideSrc, $overrideSnippet = 'SC-NewKPEntry','SC-NewKPEntry' # Use when running interactively

        . (Get-ActiveFileNm -PS1 Initialize-DataResources-Locations)

        . (Get-ActiveFileNm -PS1 Load-DataResources-Dependencies)

        #$overrideSrc, $overrideSnippet = '',''

 

    }

    process

    {

 

        . (Get-ActiveFileNm -PS1 SN-CheckKPDB)

 

        try

        {

            $KeePassGroup = Get-KpGroup -KeePassConnection $KpDB -FullPath $KPGroupPath -Stop

 

            $addKpEntrySplat = @{

                KeePassConnection = $KPDB

                KeePassGroup      = $KeePassGroup

                Title             = $Title

                UserName          = $UID

                KeePassPassword   = $EntryPwd

                Notes             = $Notes

                PassThru          = $PassThru

                URL               = $URL

                IconName          = $IconName

            }

 

            if(Test-Bound -ParameterName 'Expires'){ $addKpEntrySplat.Expires = $Expires }

            if($ExpiryTime){ $addKpEntrySplat.ExpiryTime = $ExpiryTime }

 

            Add-KpEntry @addKpEntrySplat

        }

        catch

        { Throw $_ }

    }

    end

    {

        . (Get-ActiveFileNm -PS1 SN-CloseKP)

    }

}