Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell passing argument values to parameters and back

Tags:

powershell

Alright. I'm attempting to complete a school assignment and cannot for the life of me figure this out. I'm trying to use powershell to pass values from one function to another making a "modular" type script. I can't seem to figure out how to move the values out of the scope of the function without using the $script:xxxxx. Is there another way to move the values in powershell as a regular argument parameter pass by reference?

Here's what I have:

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

The two values right after the inputGrams function should change each time the script is run but they don't because of scope issues and passing the values. Anyone know how to properly pass those values back to the main function?

like image 837
Bionic Avatar asked Feb 10 '12 03:02

Bionic


People also ask

How do I pass multiple arguments in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you pass arguments in PowerShell?

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

What is the use of $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

There are a few problems. First here's a working example:

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

And explanation:

  1. You'll need to create the variable before passing it by REF.
  2. Don't enclose PowerShell function params in parens, just separate them with spaces.
  3. Put the REF arguments in parens.
  4. To set the REF variable's value, you'll need to set the Value property.
like image 50
Andy Arismendi Avatar answered Sep 24 '22 10:09

Andy Arismendi