-1

I am new to powershell and trying to understand the Param block, I have a simple program to get the two values and print it but when I run the below code, It asks for the input secondvalue but skipped the first value?

Why does it didn't ask for the firstvalue as input?

function print {
    Param(
    [Parameter(mandatory = $true)] $firstvalue,          
    [Parameter(mandatory = $true)] $secondvalue
)
    write-host first : $firstvalue
    write-host second : $secondvalue    
}

print($firstvalue, $secondvalue)

Sample Output :

 ./first.ps1 

cmdlet print at command pipeline position 1
Supply values for the following parameters:
secondvalue: second data
first :  
second : second data

Thanks, Any help is appreciated.

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Harry
  • 3,072
  • 6
  • 43
  • 100

2 Answers2

1

Your param block looks functional to me.

I think the issue is how you're calling the function. Since both parameters are mandatory, you can just call the function by name.

function print {
    Param(
    [Parameter(mandatory = $true)] $firstvalue,          
    [Parameter(mandatory = $true)] $secondvalue
)

    write-host first : $firstvalue
    write-host second : $secondvalue    
}

print

This may help. about_Functions

Robert Rice
  • 44
  • 1
  • 5
  • Rice Thanks for your answer but I would like to know why when I pass print($firstvalue, $secondvalue) It is taking only secondvalue? – Harry Sep 24 '18 at 16:01
  • 4
    The problem is he's passing an array to the first parameter and neglecting the second parameter. Parameters in powershell are separated by spaces, not commas. @Harry When you use commas, you're passing an implicit array and an array is a single object. – Maximilian Burszley Sep 24 '18 at 16:08
  • @TheIncorrigible1 ya I got the point that comma will be considered as an array (single object), but why does it takes second value but not first value? why not it takes first value and skips second value ? – Harry Sep 24 '18 at 16:12
  • @TheIncorrigible1 also say, If I have a third value, then If I pass the parameter as print($firstvalue, $secondvalue, $thirdvalue) then it skips first value and asks for second and third value, Why powershell does this? – Harry Sep 24 '18 at 16:19
  • 1
    Stop using parentheses and commas. That is not how you call functions in powershell. You're explicitly creating an array and only passing one argument in that scenario. @Harry `print $firstvalue $secondvalue $thirdvalue` – Maximilian Burszley Sep 24 '18 at 16:25
  • got it, so the first value contains all the three :) Thanks – Harry Sep 24 '18 at 16:34
1

The core problem is that an array is being passed when print is called.

print($firstvalue, $secondvalue)

The parentheses creates an array with two elements; $firstvalue and $secondvalue. The array is interpreted as the value provided for $firstvalue, but that leaves $secondvalue with nothing. Since $secondvalue is required, the error occurs. Try using:

print $firstvalue $secondvalue
lit
  • 14,456
  • 10
  • 65
  • 119