Functions/FoldersFiles/Get-PVFileCategory.ps1

Function Get-PVFileCategory {

    <#
    .SYNOPSIS
    Lists all the File Categories at both Vault and Safe level for the
    specified file or password.

    .DESCRIPTION
    Exposes the PACLI Function: "LISTFILECATEGORIES"

    .PARAMETER vault
    The defined Vault name

    .PARAMETER user
    The Username of the authenticated User.

    .PARAMETER safe
    The name of the Safe that the File Category is attached to.

    .PARAMETER folder
    The folder containing a file with a File Category attached to it.

    .PARAMETER file
    The name of the file or password that is attached to a File Category.

    .PARAMETER category
    The name of the File Category.

    .PARAMETER sessionID
    The ID number of the session. Use this parameter when working
    with multiple scripts simultaneously. The default is ‘0’.

    .EXAMPLE
    Get-PVFileCategory -vault Lab -user administrator -safe DEV -folder Root -file TeamPass

    Lists file categories on file TeamPass in safe DEV.

    .NOTES
    AUTHOR: Pete Maan

    #>


    [CmdLetBinding()]
    param(

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True)]
        [string]$vault,

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True)]
        [string]$user,

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True)]
        [Alias("Safename")]
        [string]$safe,

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True)]
        [string]$folder,

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True)]
        [Alias("Filename")]
        [string]$file,

        [Parameter(
            Mandatory = $False,
            ValueFromPipelineByPropertyName = $True)]
        [string]$category,

        [Parameter(
            Mandatory = $False,
            ValueFromPipelineByPropertyName = $True)]
        [int]$sessionID
    )

    PROCESS {

        #execute pacli
        $Return = Invoke-PACLICommand $Script:PV.ClientPath LISTFILECATEGORIES "$($PSBoundParameters.getEnumerator() |
                ConvertTo-ParameterString) OUTPUT (ALL,ENCLOSE)"


        if($Return.ExitCode -eq 0) {

            #if result(s) returned
            if($Return.StdOut) {

                #Convert Output to array
                $Results = (($Return.StdOut | Select-String -Pattern "\S") | ConvertFrom-PacliOutput)

                #loop through results
                For($i = 0 ; $i -lt $Results.length ; $i += 3) {

                    #Get Range from array
                    $values = $Results[$i..($i + 3)]

                    #Output Object
                    [PSCustomObject] @{

                        "CategoryName"  = $values[0]
                        "CategoryValue" = $values[1]
                        "CategoryID"    = $values[2]
                        "Safename"      = $safe
                        "Folder"        = $folder
                        "Filename"      = $file

                    } | Add-ObjectDetail -TypeName pacli.PoShPACLI.File.Category -PropertyToAdd @{
                        "vault"     = $vault
                        "user"      = $user
                        "sessionID" = $sessionID
                    }

                }

            }

        }

    }

}