1
param (

                [string]$ComputerName="Server01",
                [string[]]$SEARCHTERMS=@("Unifi","Edge"),
                [string]$IPAddress='10.11.12.243',   #Dangling comma that i never see.
)

There must be a ... AND 1=1; trick like what is commonly done in sql statements to move parameters around without much thought.

rjt
  • 1,042
  • 10
  • 16
  • I don't understand what you're are asking. Are you looking to run a powershell script even with syntactic errors? also you wouldn't have such syntax problems with a proper code editor (Visual Studio Code) and the powershell preview extension. – Santiago Squarzon Nov 01 '22 at 03:37

2 Answers2

3

No, I think there is no trick that avoids this syntax error.

The only consolation is that if you use Visual Studio Code, combined with its PowerShell extension, you get near-instantaneous visual feedback that there's a syntax problem:

screenshot with highlighted syntax error

The PROBLEMS tab in the panel view (toggle with Ctrl-J) states the specific problem:
Missing expression after ','.


Making PowerShell tolerant of an extraneous (dangling) , after the last parameter declaration inside param(...) - so as to make reordering parameter declarations less painful - has been officially suggested in the past - by a former core member of the PowerShell team, no less - but the suggestion was declined:

  • See (declined) GitHub issue #8873, which also mentions not requiring , separators at all as a potential enhancement, analogous to how you can place elements inside @(...) on separate lines to form array elements, without the need for a(nother) separator.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Yes! It is exaclty like `@(arraymembers on separate lines) ` . Generally, the fewer characters on the screen make code more readable. I end up hacking on many different OSs and machines, so cant / dont want to install vsCode everywhere. Interesting history. Maybe it has something to do with using the comma to force an @(ARRAY) to pass by reference instead of copying as talked about [Braces, comma, parameter method call](https://stackoverflow.com/questions/39745848/braces-comma-parameter-method-call) – rjt Nov 01 '22 at 05:07
  • @rjt, understood re not wanting to install VSCode. In the linked post, `,` is actually an _operator_ (the [array constructor operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#comma-operator-)), which is unrelated to the parse-time-only syntactic use of `,` inside the `param(...)` block. – mklement0 Nov 02 '22 at 16:28
0
param (

     [string]$ComputerName="Server01",
     [string[]]$SEARCHTERMS=@("Unifi","Edge"),
     [string]$IPAddress='10.11.12.243', #Dangling comma that i never see.
     [string]$DumbCommas='Extra Param to Protect me from commas.'
)

Needs a shorter variable name, but it will do.

rjt
  • 1,042
  • 10
  • 16
  • Note that this has several unwanted side effects: It makes your function accept an extra argument (which matters if you use `[CmdletBinding()]` to prevent unbound arguments). The extra parameter shows up with `Get-Help` / `Get-Command -Syntax`. The extra parameter shows up in tab completion. You could mitigate these problems somewhat (preventing positional binding to your dummy parameter, giving it a name that prevents _named_ binding), but doing so requires so much extra effort that it defeats your original intent. – mklement0 Nov 02 '22 at 16:32