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

In Powershell, how to set variable values within a function and have that value available in the parent scope?

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

$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 的这些值 blank/null 在此函数之外。

正确的做法是什么?我想在此函数中设置这些值,并在稍后的父范围内的脚本中使用这些值。

powershell about_scope help document 是您想要阅读的内容。

具体这一段:

Windows PowerShell Scopes

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. :BackupFile = "Win7x64-SP1.wim"

变量是在函数的 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 进程),那么您应该使用 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"
        }
  }
}