public/Get-VPASDPAPolicyDetails.ps1

<#
.Synopsis
   GET DPA POLICY DETAILS
   CREATED BY: Vadim Melamed, EMAIL: vmelamed5@gmail.com
.DESCRIPTION
   USE THIS FUNCTION TO RETRIEVE POLICY DETAILS FROM DPA
.PARAMETER token
   HashTable of data containing various pieces of login information (PVWA, LoginToken, HeaderType, etc).
   If -token is not passed, function will use last known hashtable generated by New-VPASToken
.PARAMETER PolicyID
   UniqueID of the target policy in DPA
.PARAMETER PolicyName
   Unique name of the target policy in DPA
.EXAMPLE
   $PolicyDetails = Get-VPASDPAPolicyDetails -PolicyID {POLICY ID VALUE}
.EXAMPLE
   $PolicyDetails = Get-VPASDPAPolicyDetails -PolicyName {POLICY NAME VALUE}
.OUTPUTS
   JSON Object (PolicyDetails) if successful
   $false if failed
#>

function Get-VPASDPAPolicyDetails{
    [OutputType('System.Collections.Hashtable',[bool])]
    [CmdletBinding()]
    Param(

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=0)]
        [String]$PolicyID,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=1)]
        [String]$PolicyName,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)]
        [hashtable]$token
    )

    Begin{
        $tokenval,$sessionval,$PVWA,$Header,$ISPSS,$IdentityURL,$EnableTextRecorder,$AuditTimeStamp,$NoSSL,$VaultVersion,$HideWarnings,$AuthenticatedAs,$SubDomain = Get-VPASSession -token $token
        $CommandName = $MyInvocation.MyCommand.Name
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType COMMAND
    }
    Process{
        Write-Verbose "SUCCESSFULLY PARSED SUBDOMAIN VALUE"
        Write-Verbose "SUCCESSFULLY PARSED TOKEN VALUE"

        try{
            if($SubDomain -eq "N/A"){
                Write-VPASOutput -str "SelfHosted + PriviledgeCloud Standard solutions do not support this API Call, returning false" -type E
                $log = Write-VPASTextRecorder -inputval "SelfHosted + PrivilegeCloud Standard solutions do not support this API Call, returning false" -token $token -LogType MISC
                $log = Write-VPASTextRecorder -inputval $false -token $token -LogType RETURN
                return $false
            }

            if([String]::IsNullOrEmpty($PolicyID)){
                Write-Verbose "NO POLICY ID PROVIDED...INVOKING HELPER FUNCTION TO RETRIEVE UNIQUE POLICY ID BASED ON SPECIFIED PARAMETERS"
                $PolicyID = Get-VPASDPAPolicyIDHelper -token $token -SearchQuery $PolicyName
                Write-Verbose "RETURNING POLICY ID"
            }
            else{
                Write-Verbose "POLICY ID SUPPLIED, SKIPPING HELPER FUNCTION"
            }

            if($PolicyID -eq -1){
                $log = Write-VPASTextRecorder -inputval "COULD NOT FIND TARGET POLICY ID: $PolicyID" -token $token -LogType MISC
                $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
                Write-Verbose "COULD NOT FIND TARGET POLICY ID: $PolicyID"
                Write-VPASOutput -str "COULD NOT FIND TARGET POLICY ID: $PolicyID" -type E
                return $false
            }
            else{
                write-verbose "MAKING API CALL TO CYBERARK"
                $uri = "https://$SubDomain.dpa.cyberark.cloud/api/access-policies/$PolicyID"
                Write-Verbose "CONSTRUCTING URI: $uri"

                $log = Write-VPASTextRecorder -inputval $uri -token $token -LogType URI
                $log = Write-VPASTextRecorder -inputval "GET" -token $token -LogType METHOD

                if($sessionval){
                    $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method GET -ContentType "application/json" -WebSession $sessionval
                }
                else{
                    $response = Invoke-RestMethod -Headers @{"Authorization"=$Header} -Uri $uri -Method GET -ContentType "application/json"
                }
                $log = Write-VPASTextRecorder -inputval $response -token $token -LogType RETURNARRAY
                Write-Verbose "RETURNING POLICY DETAILS"
                return $response
            }
        }catch{
            $log = Write-VPASTextRecorder -inputval $_ -token $token -LogType ERROR
            $log = Write-VPASTextRecorder -inputval "REST API COMMAND RETURNED: FALSE" -token $token -LogType MISC
            Write-Verbose "FAILED TO RETRIEVE DPA POLICY DETAILS"
            Write-VPASOutput -str $_ -type E
            return $false
        }
    }
    End{
        $log = Write-VPASTextRecorder -inputval $CommandName -token $token -LogType DIVIDER
    }
}