小编Bio*_*nic的帖子

Powershell将参数值传递给参数并返回

好的.我正在尝试完成一项学校作业,而且不能为我的生活弄清楚这一点.我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

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

powershell

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

powershell ×1