Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在Powershell中,如何在函数中设置变量值,并使该值在父作用域中可用?_Function_Variables_Powershell_Scope - Fatal编程技术网

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

Function 在Powershell中,如何在函数中设置变量值,并使该值在父作用域中可用?,function,variables,powershell,scope,Function,Variables,Powershell,Scope,我试图使用函数设置一些变量的值。我的代码如下: $BackupFile = $null $TaskSequenceID = $null $OSDComputerName = $null $capturedWimPath = $null Function Set-OsToBuild { switch ($OsToBuild) { "Win7x64" { $BackupFile = "Win7x64-SP1.wim"

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

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


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

powershell
关于\u作用域的内容是您想要阅读的内容

具体而言,本节:

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.
因此,根据您的具体需要,我相信您可以使用以下任何一种

  • $global:BackupFile=“Win7x64-SP1.wim”
  • $script:BackupFile=“Win7x64-SP1.wim”
  • $1:BackupFile=“Win7x64-SP1.wim”

  • 变量是在函数的
    局部范围内创建的。函数完成后,这些变量将被删除

    资料来源:

    如果需要为脚本提供变量,请将它们写入
    脚本
    范围

    $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"
            }
      }
    }
    
    $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进程),则应使用
    global
    作用域

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

    侧面的相关链接显示了创建全局范围变量的方法,您可以始终返回此信息的自定义对象