forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-MyVariable2.ps1
More file actions
66 lines (50 loc) · 2.41 KB
/
Get-MyVariable2.ps1
File metadata and controls
66 lines (50 loc) · 2.41 KB
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
Function Get-MyVariable {
[cmdletbinding()]
[OutputType([System.Management.Automation.PSVariable])]
[Alias("gmv")]
Param(
[Parameter(Position = 0)]
[ValidateSet("Global", "Local", "Script", "Private", 0, 1, 2, 3)]
[ValidateNotNullOrEmpty()]
[string]$Scope = "Global",
[switch]$NoTypeInformation
)
Write-Verbose "Getting system defined variables"
#get all global variables from the initial session state
$PSVariables = [system.collections.generic.list[string]]::New()
((Get-Runspace 1).initialsessionstate.variables.name).foreach( { $PSVariables.Add($_) })
Write-Verbose "Found $($psvariables.count) initial state variables"
$skip = '?', 'args','ConsoleFileName', 'Error', 'esc',
'ExecutionContext', 'false', 'HOME', 'Host', 'input', 'MaximumAliasCount',
'MaximumDriveCount', 'MaximumErrorCount', 'MaximumFunctionCount', 'MaximumHistoryCount',
'MaximumVariableCount', 'MyInvocation', 'null', 'Passthru', 'PID', 'PROFILE',
'PSBoundParameters', 'PSCommandPath', 'PSCulture', 'PSDefaultParameterValues', 'PSEdition',
'PSGetPath', 'PSHOME', 'PSScriptRoot', 'PSUICulture', 'PSVersionTable',
'PWD', 'ShellId', 'true', 'verify', 'skip', 'scope', 'this', 'LastExitCode',
'_', 'EnabledExperimentalFeatures'
$skip.Foreach({ $PSVariables.Add($_) })
#exclude variables defined by the PSScriptTools module
$modVar = "PSAnsiFileMap","PSSamplePath","PSSpecialChar"
$modvar.Foreach({ $PSVariables.Add($_) })
<#
find all the variables where the name isn't in the variable we just created
and also isn't a system variable generated after the shell has been running
and also any from this function
#>
Write-Verbose "Getting current variables in $Scope scope"
$variables = Get-Variable -Scope $Scope
Write-Verbose "Found $($variables.count) variables"
Write-Verbose "Filtering variables"
#filter out some automatic variables
$filtered = $variables | Where-Object { $psvariables -notcontains $_.name }
if ($NoTypeInformation) {
#write results without object types
$filtered
}
else {
#add type information for each variable
Write-Verbose "Adding value type"
$filtered | Select-Object Name, Value, @{Name = "Type"; Expression = { $_.Value.GetType().Name } }
}
Write-Verbose "Finished getting my variables"
} #end function