2

I am trying to build a cool "PowerShell command" (a script containing a global function that I could later use from the shell as a command) and I have a problem.

I am using parameters with param() and there is an option to use a switch parameter (one that does not get subsequent information but just used for an option later on - such as the -l at the Linux command ls).

The thing is that it works fine when the parameter name is a letter, but not with a number. It's strange since numbers could be defined as variables ($4).

What do I need to do?

It suppose to be like ping -6.

Ideas?

4
  • 4
    You don't need to do anything - because it can't be done. The PowerShell Language Specification §2.3.4 defines parameter names as starting with a letter Commented Oct 11, 2016 at 11:14
  • 1
    Kinda funny that this constraint still exists when they allow Unicode letters as the first character ... Commented Oct 11, 2016 at 11:53
  • 1
    What is the reason you need a number as the name for a variable? If you would like to write a function like ping -6, in PowerShell would make a function like Ping {param ([ValidateSet(4,6)][int]$IPVersion) ...} and you still can use this parameter as a boolean later on. Commented Oct 11, 2016 at 12:14
  • It is a function that connects netapp with powershell, by default to the clustered on tap but I wanted a "-7" switch to connect to a 7-mode netapp Commented Oct 11, 2016 at 12:26

1 Answer 1

4

What you're asking is not possible, as Mathias pointed out in the comments.

From §2.3.4 of the language specification:

2.3.4 Parameters

Syntax:

command-parameter:
    dash   first-parameter-char   parameter-chars   colonopt
first-parameter-char:
    A Unicode character of classes Lu, Ll, Lt, Lm, or Lo
    _   (The underscore character U+005F)
    ?
…

The closest you get is something like this:

Param(
  [Parameter(Mandatory=$false)]
  [Switch]$_7
)

However, for your scenario something like this would be far more readable anyway:

Param(
  [Parameter(Mandatory=$false)]
  [ValidateSet(3,5,7)]
  [int]$Mode = 7
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.