Public/Show-VarStash.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
54
55
56
57
<#
.SYNOPSIS
   Shows the content of a variable stash.
.DESCRIPTION
   Shows the content of a variable stash. Returns objects of type PSVariable.
.EXAMPLE
   Show-VarStash
 
   Shows the content of the latest stash (from top of stash, index 0)
.EXAMPLE
   Show-VarStash -Name MyStash
 
   Shows the content of the stash named MyStash.
.EXAMPLE
   Show-VarStash -Index 2
 
   Shows the content of the stash with index position 2 (lates stash has index 0).
#>

function Show-VarStash {
    [CmdletBinding(DefaultParameterSetName = '__AllParameterSets')]
    param(
        [parameter(ParameterSetName = 'Name',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [parameter(ParameterSetName = 'Index',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(0, [int]::MaxValue)]
        [int]$Index
    )
    
    Process {

        switch ($PSCmdlet.ParameterSetName) {
            'Name' { 
                $Stash = Get-VarStash -Name $Name
                if ($Stash -is [array]) {
                    Throw 'Found more than one VarStash. Can only show one stash.'
                }
            }
            'Index' { 
                $Stash = Get-VarStash -Index $Index
            }
            Default {
                $Stash = Get-VarStash -Index 0
            }
        }

        $ImportedVariables = Import-Clixml -Path "$env:APPDATA\VariableStash\VariableStash_$($Stash.Name).xml"

        Write-Output -InputObject $ImportedVariables
        
    }
}