在Powershell中,如何在函数中设置变量值并使该值在父作用域中可用?

n3schb8v  于 2023-02-23  发布在  Shell
关注(0)|答案(3)|浏览(155)

我正在尝试使用函数设置一些变量的值。我的代码如下:

$BackupFile = $null
$TaskSequenceID = $null
$OSDComputerName = $null
$capturedWimPath = $null

Function Set-OsToBuild 
{
  switch ($OsToBuild)
  {
    "Win7x64"
        { 
            $BackupFile = "Win7x64-SP1.wim"
            $TaskSequenceID = "WIN7X64BC"
            $OSDComputerName = "Ref-Win7x64"
            $capturedWimPath = "$($PathToMdtShare)\Captures\$BackupFile"
        }
  }
}

问题在于,$BackupFile、$TaskSequenceID、$OSDComputerName和$capturedWimPath的这些值在此函数之外为空白/null。
正确的方法是什么呢?我想在这个函数中设置这些值,然后在父作用域的脚本中使用这些值。

dxpyg8gm

dxpyg8gm1#

这些变量是在函数的local-作用域中创建的,当函数结束时,这些变量会被删除。

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.

来源:about_Scopes
如果需要变量可用于脚本,则将它们写入script作用域。

$BackupFile = $null
$TaskSequenceID = $null
$OSDComputerName = $null
$capturedWimPath = $null

Function Set-OsToBuild 
{
  switch ($OsToBuild)
  {
    "Win7x64"
        { 
            $script:BackupFile = "Win7x64-SP1.wim"
            $script:TaskSequenceID = "WIN7X64BC"
            $script:OSDComputerName = "Ref-Win7x64"
            $script:capturedWimPath = "$($PathToMdtShare)\Captures\$BackupFile"
        }
  }
}

如果你想保留整个会话的值(直到你关闭powershell-process),那么你应该使用global作用域。

$global:BackupFile = $null
$global:TaskSequenceID = $null
$global:OSDComputerName = $null
$global:capturedWimPath = $null

Function Set-OsToBuild 
{
  switch ($OsToBuild)
  {
    "Win7x64"
        { 
            $global:BackupFile = "Win7x64-SP1.wim"
            $global:TaskSequenceID = "WIN7X64BC"
            $global:OSDComputerName = "Ref-Win7x64"
            $global:capturedWimPath = "$($PathToMdtShare)\Captures\$BackupFile"
        }
  }
}
1qczuiv0

1qczuiv02#

powershell about_scopehelp document就是您要阅读的内容。
特别是本节:
Windows PowerShell作用域

Scopes in Windows PowerShell have both names and numbers. The named
scopes specify an absolute scope. The numbers are relative and reflect
the relationship between scopes.

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.        

Numbered Scopes:
    You can refer to scopes by name or by a number that
    describes the relative position of one scope to another.
    Scope 0 represents the current, or local, scope. Scope 1
    indicates the immediate parent scope. Scope 2 indicates the
    parent of the parent scope, and so on. Numbered scopes
    are useful if you have created many recursive
    scopes.

因此,根据您的确切需要,您可以使用以下任何一种,我相信。

  1. $global:BackupFile = "Win7x64-SP1.wim"
  2. $script:BackupFile = "Win7x64-SP1.wim"
  3. $1:BackupFile = "Win7x64-SP1.wim"
v64noz0r

v64noz0r3#

为了在外部设置$BackupFile,您的函数应该写入$script:$BackupFile
但是有一个陷阱!如果你的脚本是从另一个脚本调用的(使用&,点源时不会发生),那么$BackupFile将从调用者继承,但$script:$BackupFile将为空。所以你应该从$BackupFile读取,但写入$script:$BackupFile。也要注意像$script:BackupFile += ".bin"这样的语法,因为它从不正确的变量读取。
避免陷阱的一个可能的解决方法是以$script:BackupFile = $BackupFile开始函数。

相关问题