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

 

if (!(Push-NeededFor UpdateKPEntry)) {

    # Remove a prior patch if it already exists

    Remove-Item -Path Function:\UpdateKPEntry

} else {

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

    $kpShortcuts = @($kpShortcuts

    (ConvertTo-DotFill $global:SCDotLen 'UpdateKPEntry')+"[-KPEntry [Obj], -KPGroupPath [Path] -Title [Title], -UID [UID], -EntryPwd [PWD]  ] "

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

    )

}

 

 

function UpdateKPEntry

{

    <#

 

        .DESCRIPTION

            This function updates a KeePass Database Entry with basic properites available for specification.

        .PARAMETER KeePassEntry

            The KeePass Entry to be updated. Use the Get-KeePassEntry function to get this object.

        .PARAMETER KPGroupPath

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

            Notes:

                * Path Separator is the foward slash character '/'

        .PARAMETER DBProfile

            *This Parameter is required in order to access your KeePass database.

        .PARAMETER Title

            Specify the Title of the new KeePass Database Entry.

        .PARAMETER UID

            Specify the UserName of the new KeePass Database Entry.

        .PARAMETER KPPwd

            *Specify the KeePassPassword 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 modified object.

        .PARAMETER Force

            Specify to Update the specified entry without confirmation.

        .PARAMETER MasterKey

            Specify a SecureString MasterKey if necessary to authenticat a keepass databse.

            If not provided and the database requires one you will be prompted for it.

            This parameter was created with scripting in mind.

        .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

            This example changes the title and the password

            $KPEntryObject = GetKPEntry -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title'

            UpdateKPEntry -KeePassEntry $KPEntryObject -KPGroupPath 'KpDB/General/Testing' -Title 'New Test Title' -Notes 'Updated with new title and pwd' -UID 'Domain\svcAccount' -EntryPwd $(NewKPPwd -upper -lower -digits -length 20) -Force

 

        .EXAMPLE (does not work)

            This example updates a keepass database entry in the 'KpDB/General/Testing' database group, with the specified Title and UserName. Also the function NewKPPwd is used to generated a random password with the specified options.

            $KPEntryObject = GetKPEntry -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title'

            UpdateKPEntry -KeePassEntry $KPEntryObject -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title' -UID 'Domain\svcAccount' -KeePassPassword $(NewKPPwd -PasswordProfileName 'Default' )

 

        .EXAMPLE

            This example updates a keepass database entry in the 'KpDB/General/Testing' database group, with the specified Title and UserName.

            $KPEntryObject = GetKPEntry -KPGroupPath 'KpDB/General/Testing' -Title 'New Test Title'

            UpdateKPEntry -KeePassEntry $KPEntryObject -KPGroupPath 'KpDB/General/Testing' -Title 'Test Title' -Notes 'Title reset and pwd reset' -EntryPWD $(ConvertTo-SecureString -String 'apassword' -AsPlainText -Force) -Force

 

            This example updates a keepass database entry with the specified Title, UserName and manually specified password converted to a securestring.

        .INPUTS

            String

            SecureString

        .OUTPUTS

            $null

    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]

    param

    (

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

        [ValidateNotNullOrEmpty()]

        [PSObject] $KeePassEntry,

 

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

        [ValidateNotNullOrEmpty()]

        [Alias('FullPath')]

        [String] $KPGroupPath,

 

        [Parameter(Position = 2)]

        [ValidateNotNullOrEmpty()]

        [String] $Title,

 

        [Parameter(Position = 3)]

        [ValidateNotNullOrEmpty()]

        [String] $UID,

 

        [Parameter(Position = 4)]

        [ValidateNotNullOrEmpty()]

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

        [PSObject] $EntryPWD,

 

        [Parameter(Position = 5)]

        [ValidateNotNullOrEmpty()]

        [String] $Notes,

 

        [Parameter(Position = 6)]

        [ValidateNotNullOrEmpty()]

        [String] $URL,

 

        [Parameter(Position = 7)]

        [ValidateNotNullOrEmpty()]

        [string] $IconName,

 

        [Parameter(Position = 8)]

        [switch] $Expires,

 

        [Parameter(Position = 9)]

        [DateTime] $ExpiryTime,

 

        [Parameter(Position = 10, ValueFromPipelineByPropertyName)]

        [ValidateNotNullOrEmpty()]

        [string] $DBProfile,

       

        [Parameter(Position = 11)]

        [Switch] $PassThru,

 

        [Parameter(Position = 12)]

        [Switch] $Force

    )

    begin

    {

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

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

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

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

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

 

    }

    process

    {

 

        . (Get-ActiveFileNm -PS1 SN-CheckKPDB)

 

        $KPEntry = Get-KPEntry -KeePassConnection $KPDB -KeePassUuid $KeePassEntry.Uuid

        if(-not $KPEntry)

        {

            Write-Warning -Message '[PROCESS] The Specified KeePass Entry does not exist or cannot be found.'

            Throw 'The Specified KeePass Entry does not exist or cannot be found.'

        }

 

        if($Force -or $PSCmdlet.ShouldProcess("Title: $($KPEntry.Strings.ReadSafe('Title')), `n`tUserName: $($KPEntry.Strings.ReadSafe('UID')), `n`tGroupPath: $($KPEntry.ParentGroup.GetFullPath('/', $true))."))

        {

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

 

            $setKPEntrySplat = @{

                URL               = $URL

                KeePassEntry      = $KPEntry

                UserName          = $UID

                Notes             = $Notes

                KeePassPassword   = $EntryPWD

                KeePassGroup      = $KeePassGroup

                PassThru          = $PassThru

                Force             = $true

                Title             = $Title

                KeePassConnection = $KPDB

            }

 

            if($IconName){ $setKPEntrySplat.IconName = $IconName }

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

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

 

            #Set-KPEntry @setKPEntrySplat | ConvertTo-KPPSObject -DBProfile $DBProfile

            Set-KPEntry @setKPEntrySplat

        }

    }

    end

    {

        . (Get-ActiveFileNm -PS1 SN-CloseKP)

    }

}