Reputation: 2950
I experience an odd behaviour on my home laptop when I create functions. The parameters are not passed into a function.
Example:
function Get-Info {
param (
$input
)
$input | gm
}
With this code (Get-Info -input 'test'
) I receive the errors:
gm : You must specify an object for the Get-Member cmdlet.
At line:5 char:14
+ $input | gm
+ ~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
I was also just trying to print a verbose statement with the parameter but I only get an empty row.
Why is the parameter not passed into the function?
Upvotes: 4
Views: 1105
Reputation: 1295
@JosefZ's comment is correct. $input is basically a reserved variable name as described in about_Automatic_Variables.
Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions).
So changing your parameter name should work as you expect. But don't forget to also change how you call the function so it also uses the new parameter name. In this case, you can also skip the parameter name entirely when calling the function
function Get-Info { param($myinput) $myinput | gm }
Get-Info -myinput 'test'
Get-Info 'test'
Upvotes: 6