PowerShell – Error – “Missing closing ‘)’ in expression” – Param // Set-StrictMode

Background

Getting an error when I try to run a PowerShell script.

The PowerScript accepts parameters and thus it includes the Param Statement.

It also enforces a Strict Mode; principally it requires that all variables should be explicitly declared.

Error Message

Here is the error message:


Missing closing ')' in expression.
+     <<<< [alias("F")]
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], ParseException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression

 

Code

Original Code

Here is the original code:

Set-StrictMode -Version 1
[CmdletBinding()]
Param
(
   [Parameter(Mandatory=$true, position=0, HelpMessage="Filename input")]
   [alias("F")]
   [string]$filename="services.txt"
)

Revised Code


[CmdletBinding()]
Param
(
	[Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
	[alias("f")]
	[string]$filename="services.txt"
)

Set-StrictMode -Version 1

Explanation

  1. The very first line needs to be the Function argument definition
    • Param
  2. And, the “Set-StrictMode -Version #” can then follow
    • Set-StrictMode -Version 1
    • Or  “Set-StrictMode -Version 2

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