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

16

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

$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。

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


侧边栏上的相关链接显示了如何创建全局作用域变量,您始终可以返回此信息的自定义对象。链接为:https://dev59.com/DUjSa4cB1Zd3GeqPFGp7?rq=1 - Matt
3个回答

22
变量是在函数的局部作用域中创建的。当函数执行完毕后,这些变量将被删除。
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"
        }
  }
}

7

如果你需要了解这个问题,你可以阅读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"

注意:以上内容涉及IT技术,翻译仅供参考,请以实际情况为准。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接