PowerShell 变量
PowerShell Variables
如果我在函数中声明了一个变量,我可以在函数外部访问该变量吗?意思是说我有一个函数将 运行 5 SQL 查询,我正在使用变量 $numberReturned
来显示每个查询返回的结果。我可以从函数外部访问 $numberReturned
吗?
$query1 = "Select * from blahblahblah"
$query2 = "Select * from fooo"
$query3 = "Select * from bar"
Execute-Query $query1
if (Execute-Query $query1)
{
if (Execute-Query $query2)
{
Execute-Query $query3
}
}
Write-Host $QueryName & $numberReturned
Function Execute-Query
{
param($QueryName)
#Stuff to Connect To SQL Server Here
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryName
#$connection is declared above in the connection stuff
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$numberReturned = $SqlAdapter.Fill($DataSet)
}
编辑---
经过进一步研究 (Mainly This Artcile),我发现我的问题全都与范围有关!
尽管 OP 已经找到了答案,但我还是把它放在这里以防 OP 中的 link 中断。
根据https://technet.microsoft.com/en-us/library/hh847849.aspx
Windows PowerShell protects access to variables, aliases, functions,
and Windows PowerShell drives (PSDrives) by limiting where they can be
read and changed...
...An item you include in a scope is visible in the scope in which it
was created and in any child scope, unless you explicitly make it
private. You can place variables, aliases, functions, or Windows
PowerShell drives in one or more scopes.
例如,在脚本级别声明的变量将可以在脚本中定义的函数内部访问。
在函数内部声明的变量将无法在该函数外部访问,除非特别说明
以允许访问的方式声明,例如声明变量全局。
下面是相同 link 范围的分解:
Global: The scope that is in effect when Windows PowerShell starts.
Variables and functions that are present when Windows PowerShell
starts have been created in the global scope. This includes automatic
variables and preference variables. This also includes the variables,
aliases, and functions that are in your Windows PowerShell profiles.
Local: The current scope. The local scope can be the global scope
or any other scope.
Script: The scope that is created while a script file runs. Only
the commands in the script run in the script scope. To the commands
in a script, the script scope is the local scope.
Private: Items in private scope cannot be seen outside of the current
scope. You can use private scope to create a private version of an
item with the same name in another scope.
这是一般信息。在这一点之后,它变得比这个问题所必需的更复杂。 link 包含在顶部,供任何需要深入了解的人使用。由于原始发布者已经弄清楚访问相关变量的能力取决于声明的 how/where 。在这种情况下,只需在函数外部声明变量就足够了。
如果我在函数中声明了一个变量,我可以在函数外部访问该变量吗?意思是说我有一个函数将 运行 5 SQL 查询,我正在使用变量 $numberReturned
来显示每个查询返回的结果。我可以从函数外部访问 $numberReturned
吗?
$query1 = "Select * from blahblahblah"
$query2 = "Select * from fooo"
$query3 = "Select * from bar"
Execute-Query $query1
if (Execute-Query $query1)
{
if (Execute-Query $query2)
{
Execute-Query $query3
}
}
Write-Host $QueryName & $numberReturned
Function Execute-Query
{
param($QueryName)
#Stuff to Connect To SQL Server Here
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryName
#$connection is declared above in the connection stuff
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$numberReturned = $SqlAdapter.Fill($DataSet)
}
编辑--- 经过进一步研究 (Mainly This Artcile),我发现我的问题全都与范围有关!
尽管 OP 已经找到了答案,但我还是把它放在这里以防 OP 中的 link 中断。
根据https://technet.microsoft.com/en-us/library/hh847849.aspx
Windows PowerShell protects access to variables, aliases, functions, and Windows PowerShell drives (PSDrives) by limiting where they can be read and changed...
...An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or Windows PowerShell drives in one or more scopes.
例如,在脚本级别声明的变量将可以在脚本中定义的函数内部访问。 在函数内部声明的变量将无法在该函数外部访问,除非特别说明 以允许访问的方式声明,例如声明变量全局。
下面是相同 link 范围的分解:
Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.
Local: The current scope. The local scope can be the global scope or any other scope.
Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
Private: Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.
这是一般信息。在这一点之后,它变得比这个问题所必需的更复杂。 link 包含在顶部,供任何需要深入了解的人使用。由于原始发布者已经弄清楚访问相关变量的能力取决于声明的 how/where 。在这种情况下,只需在函数外部声明变量就足够了。