簡體   English   中英

在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的這些值在此函數之外是空白/ null。

這樣做的正確方法是什么? 我想在此函數中設置這些值,並在稍后的父作用域中的腳本中提供這些值。

變量在函數的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-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"
        }
  }
}

powershell about_scope 幫助文檔是您想要閱讀的內容。

具體這一部分:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM