functions/resultcache/Get-PSFResultCache.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function Get-PSFResultCache
{
<#
    .SYNOPSIS
        Returns the last stored result
     
    .DESCRIPTION
        Functions that implement the result cache store their information in the cache. This can then be retrieved by the user running this command.
        This forgives the user for forgetting to store the output in a variable and is especially precious when running commands that take a while to execute.
     
    .PARAMETER Type
        Default: Value
        Options: All, Value
        By default, this function will return the output that was cached during the last execution. However, this mode can be switched:
        - All: Returns everything that has been cached. This includes the name of the command calling Set-PFSResultCache as well as the timestamp when it was called.
        - Value: Returns just the object(s) that were written to cache
     
    .EXAMPLE
        PS C:\> Get-PSFResultCache
     
        Returns the latest cached result.
     
    .EXAMPLE
        PS C:\> Get-PSFResultCache -Type 'All'
     
        Returns a meta-information object containing the last result, when it was written and which function did the writing.
#>

    
    [CmdletBinding()]
    param (
        [ValidateSet('Value','All')]
        [string]
        $Type = 'Value'
    )
    
    switch ($Type)
    {
        'All'
        {
            New-Object PSObject -Property @{
                Result    = ([PSFramework.ResultCache.ResultCache]::Result)
                Function  = ([PSFramework.ResultCache.ResultCache]::Function)
                Timestamp = ([PSFramework.ResultCache.ResultCache]::Timestamp)
            }
        }
        'Value'
        {
            [PSFramework.ResultCache.ResultCache]::Result
        }
    }
}
if (-not (Test-Path "alias:Get-LastResult")) { New-Alias -Name Get-LastResult -Value Get-PSFResultCache -Description "A more intuitive name for users to call Get-PSFResultCache" }
if (-not (Test-Path "alias:glr")) { New-Alias -Name glr -Value Get-PSFResultCache -Description "A faster name for users to call Get-PSFResultCache" }