4

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?

2
  • 4
    The $input variable has a special meaning. Read Get-Help about_Automatic_Variables. Commented Sep 15, 2019 at 16:20
  • @JosefZ, thanks for the hint. I changed the parameter name but receive the same odd behaviour. Commented Sep 15, 2019 at 16:33

2 Answers 2

6

@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'
Sign up to request clarification or add additional context in comments.

Comments

-1

Try this way. This [String] | gm

2 Comments

It may be helpful to format the text and code in your answer differently. Would this be a way to format your answer (using backticks (``) for code)? "Try this way. This [String] | gm."
The OP wants to inspect the type of the value stored in the $input variable, so passing type literal [string] instead won't help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.