Debugging/Troubleshooting/Validating PSUCache

Product: PowerShell Universal
Version: 1.5.14

Through the course of using/testing in PSU, I have come to find myself sometimes needing to validate the contents of PSUCache, I wrote these functions for retrieving data from there:

function Get-PSUCacheKeys {
    param(
        [string]
        $ServerUri = ${PSU.Server.Uri},
        [string]
        $AppToken = ${PSU.Secret.AdminAppToken}
    )
    $Uri = $ServerUri + '/api/v1/cache/key'
    $Headers = @{
        accept        = 'text/plain'
        Authorization = "Bearer $AppToken"
    }
    Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers
}
function Get-PSUCacheByKey {
    param (
        [string]
        $Key,
        [string]
        $ServerUri = ${PSU.Server.Uri},
        [string]
        $AppToken = ${PSU.Secret.AdminAppToken}
    )
    $CacheKeys = Get-PSUCacheKeys -ServerUri $ServerUri -AppToken $AppToken
    if ($Key -in $CacheKeys) {
        $Uri = ${PSU.Server.Uri} + "/api/v1/cache/$key/value"
        $Headers = @{
            accept        = '*/*'
            Authorization = "Bearer $AppToken"
        }
        $Return = Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers
    }
    [System.Management.Automation.PSSerializer]::Deserialize($Return.CliXml)
}


function Get-PSUCacheData {
    param (
        [string]
        $ServerUri = ${PSU.Server.Uri},
        [string]
        $AppToken = ${PSU.Secret.AdminAppToken}
    )
    $PSUCacheKeys = Get-PSUCacheKeys -ServerUri $ServerUri -AppToken $AppToken
    $CacheObject = [PSCustomObject]::new()
    $PSUCacheKeys.ForEach{
        $Key = $PSItem 
        $OldEAP = $ErrorActionPreference
        $ErrorActionPreference = 'SilentlyContinue'
        $Value = Get-PSUCacheByKey -Key $Key -ServerUri $ServerUri -AppToken $AppToken -ErrorAction 'SilentlyContinue'
        if ($null -ne $Value) {
            $CacheObject | Add-Member -Name $Key -MemberType NoteProperty -Value $Value -Force
        }
        $ErrorActionPreference = $OldEAP
    }
    $CacheObject
}

NOTE: ${PSU.Server.Uri} and ${PSU.Secret.AdminAppToken} are variables I have set in my dev environment. If you set these, you won’t have to manually use them when calling the functions.

Enjoy!

1 Like