Home > Blockchain >  Is there a trick to never have to worry about a dangling comma in PowerShell param statements?
Is there a trick to never have to worry about a dangling comma in PowerShell param statements?

Time:11-01

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.

CodePudding user response:

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

The only consolation is that if you use 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.

CodePudding user response:

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.

  • Related