Public/Get-VarStashBlackList.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
82
83
84
85
86
87
<#
.SYNOPSIS
   Gets the variable stash black list.
.DESCRIPTION
   Gets the variable stash black list. The black list contains variable names which will be ignored when pushing to variable stash.
   These names are standard variables which are not user created.
.EXAMPLE
   Get-VarStashBlackList
 
   Gets the variable stash black list.
#>

function Get-VarStashBlackList {
    [CmdletBinding()]
    param()

    Process {
        # Static list: https://ss64.com/ps/syntax-automatic-variables.html
        $static = '$',
            '?',
            '^',
            '_',
            'Allnodes',
            'Args',
            'ConsoleFileName',
            'Error',
            'Event',
            'EventArgs',
            'EventSubscriber',
            'ExecutionContext',
            'False',
            'ForEach',
            'Home',
            'Host',
            'Input',
            'LastExitCode',
            'Matches',
            'MyInvocation',
            'NestedPromptLevel',
            'NULL',
            'OFS',
            'PID',
            'Profile',
            'PSBoundParameters',
            'PsCmdlet',
            'PSCommandPath',
            'PsCulture',
            'PSDebugContext',
            'PsHome',
            'PSitem',
            'PSScriptRoot',
            'PSSenderInfo',
            'PsUICulture',
            'PsVersionTable',
            'Pwd',
            'Sender',
            'ShellID',
            'SourceArgs',
            'SourceEventArgs',
            'StackTrace',
            'This',
            'True'

        # Manually added
        $ManuallyAdded = 'MaximumHistoryCount',
            'PSDefaultParameterValues',
            'psEditor'

        
        # Variables from inital session state
        $RunspaceSessionState = (Get-Runspace).InitialSessionState.Variables.Name

        # Mash them together
        $BlackList = [System.Collections.Generic.HashSet[String]]::new()
        foreach ($item in $static) {
            $null = $BlackList.Add($item)
        }
        foreach ($item in $ManuallyAdded) {
            $null = $BlackList.Add($item)
        }
        foreach ($item in $RunspaceSessionState) {
            $null = $BlackList.Add($item)
        }

        # Done
        Write-Output $BlackList
    }
}