Public/Push-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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
   Pushes variables to the variable stash.
.DESCRIPTION
   Pushes variables to the variable stash. The pushed variables will be stored on the top of the stash (index 0).
.EXAMPLE
   Push-VariableStash
 
   Pushes variables to the variable stash. Since the -Name parameter is omitted, a default name will be set (a guid).
.EXAMPLE
   Push-VariableStash -Name MyStash
 
   Pushes variables to the variable stash with name MyStash.
#>

function Push-VarStash {
    [CmdletBinding()]
    param (
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
            {
                $_.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars() + [char]'_') -eq -1
            },
            ErrorMessage = 'Name must have valid file name characters and cannot contain underscore.')]
        [string]$Name
    )

    Process {

        # Get variables
        # Scope need to be 2 when running as a function inside a module.
        # Scope need to be 1 when running as a function not part of a module.
        try {
            $AllVariables = Get-Variable -Scope 2
        }
        catch [System.Management.Automation.PSArgumentOutOfRangeException] {
            try {
                Write-Verbose "Using scope 1"
                $AllVariables = Get-Variable -Scope 1
            }
            catch {
                Throw $_
            }
        }
        catch {
            Throw "Unable to get variables. Error: $_" 
        }

        # Get blacklist
        $Blacklist = Get-VarStashBlackList

        # Remove blacklisted variables
        $ApprovedVariables = $AllVariables.Where({$_.Name -notin $Blacklist})
        
        # Remove variables which are read only, constants or private
        # https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.scopeditemoptions?view=powershellsdk-7.0.0
        $WritableApprovedVariables = $ApprovedVariables.Where({$_.Options -notin 1, 2, 9, 10})
        
        # Any variables left?
        if ($WritableApprovedVariables.Count -eq 0) {
            Write-Error -Message 'No variables to stash.' -Category ObjectNotFound 
        }
        else {
            # Generate filename
            if ([string]::IsNullOrEmpty($Name)) {
                $FileName = "VariableStash_$((New-Guid).Guid).xml"
            }
            else {
                $FileName = "VariableStash_$Name.xml"
            }

            # Prepare folder if needed
            if (-not (Test-Path -Path "$env:APPDATA\VariableStash" -PathType 'Container')) {
                $null = New-Item -Path $env:APPDATA -Name 'VariableStash' -ItemType 'Container'
            }

            # Write to disk
            Export-Clixml -InputObject $WritableApprovedVariables -Path "$env:APPDATA\VariableStash\$FileName"
        }

    }
}