【发布时间】: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