Microsoft – Powershell – Parameters (Command Line)

Microsoft – Powershell – Parameters (Command Line)

Back on this Microsoft Powershell grind.  Need to do a quick script.

But, really stumbling….

For instance, I will like to formularize my command line parameters.  Have clean well defined names for them.

In most programming languages you end up with dummied-down numbered augment list i.e. %1 %2, etc.

But, PowerShell supports named command line arguments.

I think the way to do is:

On the try first line of your script, enter something that resembles the following:

 Param (
         [parameter(
                      Mandatory=$false
                    , position=0
                    , HelpMessage="The machines's IPAddress"
                   )
          ]

          [alias("I")]

          [string] $IPAddress = "localhost"

 

And, you ‘re saying that is simple. What is the “big deal”…

That line “what is the big deal” is a line from my young niece. I was visiting her Dad’s graduation and did the simplest of things – Brought the young girl an Ice-Cream. Her Daddy kept saying she should Thank Me. Finally, she had enough, and wanted to know what is such a big deal about “Thanking her Uncle” for a $2 ice-cream.

Well in this case, PowerShell’s Language Parser is a little picky.

Sample 1:

 Param (
         [ parameter(
                      Mandatory=$false
                    , position=0
                    , HelpMessage="The machines's IPAddress"
                   )
          ]

          [alias("I")]

          [string] $IPAddress = "localhost"

 

In case, you have a space between the left braces and the parameter keyword, you will get an error message stating:

Cannot find the type for custom attribute ‘parameter’. Make sure that the assembly that contains this type is loaded.

FullyQualifiedErrorId : CustomAttributeTypeNotFound

Sample 2:

 Param (
         [parameter (
                      Mandatory=$false
                    , position=0
                    , HelpMessage="The machines's IPAddress"
                   )
          ]

          [alias("I")]

          [string] $IPAddress = "localhost"

In case, you have a space between the parameter keyword and the left parentheses, you will get a similar error message.

Sample 3:

 Param (
         [parameter (
                      Mandatory=$false
                    , position=0
                    , HelpMessage="The machines's IPAddress"
                   )
          ]

          [alias("IP")]

          [string] $IPAddress = "localhost"

In case, you try to get a bit fancy with your Alias names.

For being fanciful, you will get:

Parameter alias cannot be specified because an alias with the name ‘IP’ was defined multiple times for the command.

It seems parameter names can only be a single character.

Sample 4:

 # Set Strict Mode to Version 2
 Set-StrictMode -version 2 

 Param (
         [parameter (
                      Mandatory=$false
                    , position=0
                    , HelpMessage="The machines's IPAddress"
                   )
          ]

          [alias("I")]

          [string] $IPAddress = "localhost"

In case, you try to use the “Set-Strict Mode” API (Establishes and enforces coding rules in expressions, scripts, and script blocks – http://technet.microsoft.com/en-us/library/dd347614.aspx).

For trying to do, prior to establishing our command line argument structure, we get an error (such as):

Missing closing ‘)’ in expression.

It seems our main function declaration has to be the very first non-comment line.

You ‘re probably saying read the “Damn Documentation…”.

I know….

But, for times such as this, when & where we are all learning together… who needs the seclusion of a private reading.

Thanks to Richard Siddaway for blogging about the same error a little over 2 years ago.  It has been a long time, but his “public notebook” helped.

On reviewing Richard’s Blog, I see that he blogged 65 items in March and 94 times in Feb 2012.  Do these guys sleep or is it just that their pens write faster than others…

References:

  1. PowerShell Advanced Function Parameters http://richardspowershellblog.wordpress.com/2009/02/21/powershell-advanced-function-parameters/

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s