<# FPS:CONTAINERS:ALOHA-P1:DATARESOURCES:PRIVATE:GET-KPGROUP:ACTIVE #>

 

 

function Get-KPGroup {

 

<#

 

.SYNOPSIS

Container Module: DataResources

Private CmdLet: Get-KPGroup

Dependencies: Yes

 

.DESCRIPTION

Gets a KeePass Group Object. Type: KeePassLib.PwGroup

   

.USAGE

 PARAMETER KeePassConnection

           Specify the Open KeePass Database Connection

 

           See Get-KeePassConnection to Create the conneciton Object.

 PARAMETER FullPath

           Specify the FullPath of a Group or Groups in a KPDB

 PARAMETER GroupName

           Specify the GroupName of a Group or Groups in a KPDB.

 PARAMETER KeePassUuid

           Specify the Uuid of the Group.

 

.EXAMPLE

 

# This Example will return a KeePassLib.PwGroup array Object with the full group path specified.

$ThisDependency = "Get-KPGroup";. (Get-ActiveFileNm -PS1 Load-Dependency)

. (Get-ActiveFileNm -PS1 SN-CheckKPDB)

Get-KPGroup -KeePassConnection $KpDB -FullPath 'kpdb/groups/servicenow'

 

# This Example will return a KeePassLib.PwGroup array Object with the groups that have the specified name.

$ThisDependency = "Get-KPGroup";. (Get-ActiveFileNm -PS1 Load-Dependency

. (Get-ActiveFileNm -PS1 SN-CheckKPDB)       

Get-KPGroup -KeePassConnection $KpDB -GroupName 'servicenow'

 

# This Example will return a KeePassLib.PwGroup array Object with the group used for testing in debug mode

$ThisDependency = "Get-KPGroup";. (Get-ActiveFileNm -PS1 Load-Dependency) 

. (Get-ActiveFileNm -PS1 SN-CheckKPDB)      

Get-KPGroup -KeePassConnection $KpDB -FullPath 'KpDB/General/Testing' -dbg

     

.CC

 

[2024.12.30 JD] Original code integration to FPS

 

.NOTES

 

 

#>

 

    [CmdletBinding(DefaultParameterSetName = 'None')]

    [OutputType('KeePassLib.PwGroup')]

    param

    (

        [Parameter(Position = 0, Mandatory, ParameterSetName = 'Full')]

        [Parameter(Position = 0, Mandatory, ParameterSetName = 'Partial')]

        [Parameter(Position = 0, Mandatory, ParameterSetName = 'None')]

        [ValidateNotNullOrEmpty()]

        [KeePassLib.PwDatabase] $KeePassConnection,

 

        [Parameter(Position = 1, Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'Full')]

        [ValidateNotNullOrEmpty()]

        [String] $FullPath,

 

        [Parameter(Position = 1, Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'Partial')]

        [ValidateNotNullOrEmpty()]

        [String] $GroupName,

 

        [Parameter(Position = 2)]

        [Switch] $Stop,

 

        [Parameter(Position = 3)]

        [Switch] $Dbg

    )

    begin

    {

        try

        {

            [KeePassLib.PwGroup[]] $KeePassOutGroups = $null

            [KeePassLib.PwGroup[]] $KeePassGroups = $KeePassConnection.RootGroup

            $KeePassGroups += $KeePassConnection.RootGroup.GetFlatGroupList()

        }

        catch

        {

            Write-Warning -Message 'An error occured while getting a KeePassLib.PwGroup Object.'

            Write-Error -ErrorRecord $_ -ea Stop

        }

    }

    process

    {

 

        [int] $foundCount = 0

 

        if ($PSBoundParameters.Dbg -eq $True) { Write-Host ( '$PSCmdlet.ParameterSetName = '+$PSCmdlet.ParameterSetName ) }

 

        if($PSCmdlet.ParameterSetName -eq 'Full')

        {

            foreach($_keepassGroup in $KeePassGroups)

            {

                # write-host ('kpgr '+$_keepassGroup.GetFullPath('/', $true).ToLower())

                if($_keepassGroup.GetFullPath('/', $true).ToLower().Equals($FullPath.ToLower()))

                {

                    $_keepassGroup

                    $foundCount += 1

                } else {

                    if ($PSBoundParameters.Dbg -eq $True) { Write-Host ('Fail: '+$_keepassGroup.GetFullPath('/', $true).ToLower()) }

                }

            }

        }

        elseif($PSCmdlet.ParameterSetName -eq 'Partial')

        {

            foreach($_keepassGroup in $KeePassGroups)

            {

                if($_keepassGroup.Name.ToLower().Equals($GroupName.ToLower()))

                {

                    $_keepassGroup

                    $foundCount += 1

                } else {

                    if ($PSBoundParameters.Dbg -eq $True) { Write-Host ('Fail: '+$_keepassGroup.Name.ToLower()) }

                }

            }

        }

        elseif($PSCmdlet.ParameterSetName -eq 'None')

        {

            $KeePassGroups

            $foundCount = $KeePassGroups.Count

        } else {

            if ($PSBoundParameters.Sync -eq $dbg) { Write-Host ('Fail: '+$_keepassGroup.Name.ToLower()) }

        }

 

        if($Stop -and $foundCount -eq 0)

        {

            Write-Warning -Message ('[PROCESS] The Specified KeePass Entry Group Path ({0}) does not exist.' -f $KeePassGroupParentPath)

            Throw 'The Specified KeePass Entry Group Path ({0}) does not exist.' -f $KeePassGroupParentPath

        }

    }

}