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



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

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

正确的方法是什么?我想在此函数中设置这些值,并稍后在脚本的父作用域中提供这些值。

变量

是在函数的local -scope 中创建的。函数完成后,这些变量将被删除。

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"
        }
  }
}

powershell about_scope帮助文档是你想要阅读的内容。

具体来说,本节:

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

为了在外部设置$BackupFile,您的函数应该写入 $script:$BackupFile

但有一个问题!如果您的脚本是从另一个脚本调用的(使用 & ,则在点源时不会发生),则$BackupFile将从调用方继承,但$script:$BackupFile将为空。所以你应该从$BackupFile阅读,但写信给$script:$BackupFile.还要注意像$script:BackupFile += ".bin"这样的语法,因为它从不正确的变量读取。

避免陷阱的可能解决方法是从 $script:BackupFile = $BackupFile 开始函数。

最新更新