PowerShell advanced function parameters

One annoying error I have just stumbled over.  When using the Parameter keyword don’t put a space after it.  If you do this

[Parameter (Position=0,HelpMessage="The length of password. Default is random between 8 and 12")]

you will get this error

Cannot find type for custom attribute ‘Parameter ‘. Please make sure the assembly containing this type is loaded.
At C:Scriptswippassword.ps1:29 char:20
+         [Parameter  <<<< (Position=0,HelpMessage="The length of password. Default is random between 8 and 12")]
    + CategoryInfo          : InvalidOperation: (Parameter :Token) [], RuntimeException
    + FullyQualifiedErrorId : CustomAttributeTypeNotFound

for it to run correctly you need to use

[Parameter(Position=0,HelpMessage="The length of password. Default is random between 8 and 12")]

notice that the opening ( comes immediately after the word Parameter

One to watch

 

This entry was posted in PowerShell V2. Bookmark the permalink.

4 Responses to PowerShell advanced function parameters

  1. Douglas says:

    Good to know. Here strings suffer a similar fate.$x = @""@spaces after the quote on the first line cause an error.

  2. Richard says:

    Here strings should have nothing after the opening quote but a line feed\carriage returnBeware copying here strings from blog posts etc you can often introduce a space after the opening quote which can be difficult to track down

  3. Pingback: Microsoft – Powershell – Parameters (Command Line) | Daniel Adeniji's – Learning in the Open

  4. danieladeniji says:

    Thanks. Save me a big headache. Now I can go without the Aspirin.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s