Powershell将参数值传递给参数并返回
好的。我正试图完成一项学校作业,但我一辈子都搞不懂这一点。我正在尝试使用powershell将值从一个函数传递到另一个函数,从而生成一个“模块化”类型的脚本。我似乎不知道如何在不使用$script:xxxxx的情况下将值移出函数的范围。是否有其他方法可以将powershell中的值作为常规参数参数按引用传递 以下是我所拥有的:Powershell将参数值传递给参数并返回,powershell,Powershell,好的。我正试图完成一项学校作业,但我一辈子都搞不懂这一点。我正在尝试使用powershell将值从一个函数传递到另一个函数,从而生成一个“模块化”类型的脚本。我似乎不知道如何在不使用$script:xxxxx的情况下将值移出函数的范围。是否有其他方法可以将powershell中的值作为常规参数参数按引用传递 以下是我所拥有的: function main { inputGrams($carbGrams, $fatGrams) $carbGrams $carbGrams calcGrams dis
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函数后面的两个值应该在每次运行脚本时更改,但由于范围问题和传递值的原因,它们不会更改。有人知道如何正确地将这些值传递回主函数吗?有几个问题。首先,这里有一个工作示例:
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
和说明:
Andy走的是正确的道路,但是[Ref]很棘手,如果可以的话,建议避免使用它们 正如你所说,问题在于范围。所有函数(除main外)都是从main调用的,因此您需要通过在main的作用域(即它们的父作用域)中使用Set Variable或New 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
使用Get变量检索它们的值时,同一点有效
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
我希望我没有破坏你的作业,只是想帮你;) 参考避免建议?Bruce的例子表现得像我期望的那样。。。我自己不会选择裁判,但是我认为这对于OP的场景来说是好的。另一方面,我通常会尝试使用函数返回值,如果希望它生成一些非常容易看到数据在什么范围内的东西。确实,我只在COM中使用[ref],例如Office的API,并且只有在必要时才在函数/脚本中使用它们,例如标记化。希望OP能更好地掌握PowerShell中最棘手的两个问题,[ref]和作用域。使用COM也可能非常棘手:-(请查看。还有一些避免使用[ref]的其他方法)是在父作用域中使用哈希表来存储值,还是编写函数来使用哈希表键而不是离散变量,或者使用点源(.function)在当前作用域中运行函数家庭作业、计算营养信息还是编写PowerShell脚本?如果后者我喜欢你的学校:-)