【问题标题】:Powershell passing argument values to parameters and backPowershell将参数值传递给参数并返回
【发布时间】:2012-02-10 03:24:36
【问题描述】:

好的。我正在尝试完成一项学校作业,但我一生都无法弄清楚这一点。我正在尝试使用 powershell 将值从一个函数传递到另一个函数,从而制作一个“模块化”类型的脚本。我似乎无法弄清楚如何在不使用 $script:xxxxx 的情况下将值移出函数范围。是否有另一种方法可以将 powershell 中的值作为常规参数参数通过引用传递?

这是我所拥有的:

function main
{
inputGrams($carbGrams, $fatGrams)
$carbGrams
$carbGrams
calcGrams
displayInfo
}

function inputGrams([ref]$carbGrams, [ref]$fatGrams)
{
    $carbGrams = read-host "Enter the grams of carbs per day"
    $fatGrams = read-host "Enter the grams of fat per day"
}

function calcGrams
{
    $carbCal = $carbGrams * 4
    $fatCal = $fatGrams * 9
}

function displayInfo 
{
    write-host "The total amount of carb calories is $carbCal" 
    write-host "The total amount of fat calories is $fatCal"
}

main

inputGrams 函数之后的两个值应该在每次脚本运行时更改,但它们不会因为范围问题和传递值而改变。任何人都知道如何正确地将这些值传递回主函数?

【问题讨论】:

  • 家庭作业是哪一部分,计算营养信息还是编写PowerShell脚本?如果后者我喜欢你的学校:-)

标签: powershell


【解决方案1】:

有几个问题。首先是一个工作示例:

function main
{
    # 1. Create empty variable first.
    New-Variable -Name carbGrams
    New-Variable -Name fatGrams

    # 2. Spaces in between parameters. Not enclosed in parens.
    # 3. Put REF params in parens.
    inputGrams ([ref]$carbGrams) ([ref]$fatGrams)

    $carbGrams
    $fatGrams
}

function inputGrams( [ref]$carbGrams, [ref]$fatGrams )
{
    # 4. Set the Value property of the reference variable.
    $carbGrams.Value = read-host "Enter the grams of carbs per day"
    $fatGrams.Value = read-host "Enter the grams of fat per day"
}

main

及解释:

  1. 您需要先创建变量,然后再通过 REF 传递它。
  2. 不要将 PowerShell 函数参数括在括号中,只需用空格隔开即可。
  3. 将 REF 参数放在括号中。
  4. 要设置 REF 变量的值,您需要设置 Value 属性。

【讨论】:

    【解决方案2】:

    Andy 走在正确的道路上,但 [Ref] 很棘手,建议尽可能避免使用它们

    正如您所说,问题在于范围。你的所有函数——除了 ma​​in——都是从 main 调用的,因此你需要通过将变量设置在 ma​​in 的作用域(即它们的父作用域)来使这些函数可用, 使用 Set-Variable 或 New-Variable。

    使用 Get-Variable 检索它们的值时,同一点有效。

    function main
    {
        inputGrams
        $carbGrams
        $fatGrams
        calcGrams
        displayInfo
    }
    
    function inputGrams
    {
        # type constrain as Single because Read-Host returns a String
        [single]$carbs = read-host "Enter the grams of carbs per day"
        [single]$fat = read-host "Enter the grams of fat per day"
    
        # scope 1 is the parent scope, i.e. main's scope
        Set-Variable -Name carbGrams -Value $carbs -Scope 1
        Set-Variable -Name fatGrams -Value $fat -Scope 1
    }
    
    function calcGrams
    {
        # scope 1 is the parent scope, i.e. main's scope
        Set-Variable -Name carbCal -Value ($carbGrams * 4) -Scope 1
        Set-Variable -Name fatCal -Value ($fatGrams * 9) -Scope 1
    }
    
    function displayInfo 
    {
        # scope 1 is the parent scope, i.e. main's scope
        $_carbCal = Get-Variable -Name carbCal -Scope 1 -ValueOnly
        $_fatCal = Get-Variable -Name fatCal -Scope 1 -ValueOnly
    
        write-host "The total amount of carb calories is $_carbCal" 
        write-host "The total amount of fat calories is $_fatCal"
    }
    
    main
    

    PS:我希望我没有破坏你的学校作业,只是想帮忙;)

    【讨论】:

    • 参考回避建议?
    • 布鲁斯的例子表现得像我预期的那样......我自己不会选择 REF 但我认为这对于 OP 的场景来说很好。另一方面,我通常会尝试让函数返回值,如果它预期会产生一些很容易看到数据在什么范围内的东西。
    • 没错,我只在 COM 中使用 [ref],例如 Office 的 API,并且只有在必要时才在函数/脚本中使用它们,例如标记化。希望 OP 能够更好地掌握 PowerShell 中两个最棘手的问题,[ref] 和范围。
    • 使用 COM 也很棘手 :-( 检查 this
    • 避免使用 [ref] 的其他几种方法是在父作用域中使用哈希表来存储值,编写函数以使用哈希表键而不是离散变量,或者使用点源(.function)在当前范围内运行函数
    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2023-03-26
    • 2019-02-09
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多