forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTest-Variable.ps1
83 lines (63 loc) · 1.54 KB
/
Test-Variable.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
<#
.SYNOPSIS
Indicates whether a variable has been defined.
.INPUTS
System.String name of a variable.
.OUTPUTS
System.Boolean indicating whether the variable name is defined.
.FUNCTIONALITY
PowerShell
.LINK
Add-ScopeLevel.ps1
.LINK
Get-Variable
.EXAMPLE
Test-Variable.ps1 true
True
.EXAMPLE
Test-Variable.ps1 ''
False
A variable can't have an empty string for a name.
.EXAMPLE
Test-Variable.ps1 $null
False
A variable can't have a null name.
.EXAMPLE
Test-Variable.ps1 null
True
.EXAMPLE
'PSVersionTable','false' |Test-Variable.ps1
True
True
.EXAMPLE
'PWD','PID' |Test-Variable.ps1 -Scope Global
True
True
#>
#Requires -Version 3
[CmdletBinding()][OutputType([bool])] Param(
# A variable name to test the existence of.
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)][AllowEmptyString()][AllowNull()][string] $Name,
# The scope of the variable to test, Global, Local, Script, or the number of a calling parent context.
[Parameter(Position = 1)][string] $Scope
)
Process
{
if ($Name -in $null, '')
{
return $false
}
elseif (!$Scope)
{
if (Get-Variable -Name $Name -ErrorAction Ignore) { return $true }
Write-Debug "$($MyInvocation.MyCommand.Name): $Name not found in default scope"
return $false
}
else
{
$Scope = Add-ScopeLevel.ps1 $Scope
if (Get-Variable -Name $Name -Scope $Scope -ErrorAction Ignore) { return $true }
Write-Debug "$($MyInvocation.MyCommand.Name): $Name not found in $Scope scope"
return $false
}
}