1

I'm have a script which runs just fine at work but when I run the same script at home to build on it, the script fails with the following error:

Unexpected token '(' in expression or statement.
At C:\Users\MyAccount\Documents\Test.ps1:34 char:54
+ $Log = [Microsoft.VisualBasic.Interaction]::InputBox ('Enter the Even ...
Unexpected token '(' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Here is the code which I am trying to run:

$log = [Microsoft.VisualBasic.Interaction]::InputBox ('Enter the Event ID you are looking for.', 'Event ID Query')
cd C:\temp\winevent_logs
$List = Get-ChildItem | Where-Object {$_.FullName -match "security"} | Select-Object
Get-WinEvent -FilterHashtable @{path=$List;id=$Log} | Measure-Object | Select-Object count

I'm guessing there's a configuration issue with my home system which is not interpreting Visual Basic operations, but I'm not sure where to start with this.

0

2 Answers 2

3

You have a syntax problem (which means that your problem is unrelated to which machine you run your code on):

  • When calling .NET methods in PowerShell, there must be no space between the method name and the opening parenthesis , (, unlike in C#, where this is allowed.

Using the [string] type's .ToUpper() instance method as a simple example:

# OK - no space between 'ToUpper' and '('
PS> 'foo'.ToUpper()
FOO

# BROKEN - space before '('
PS> 'foo'.ToUpper ()

ParserError:
Line |
   1 |  'foo'.ToUpper ()
     |                ~
     | Unexpected token '(' in expression or statement.

Note:

  • With the mistaken space, PowerShell interprets what follows the space ((), in this case) as a separate expression, which breaks the syntax, given that a space-separated list of expressions (that aren't connected with operators) is invalid; e.g., 1 2 triggers the same error.

  • As an aside: omitting () in what would otherwise be a method call - e.g., 'foo'.ToUpper - serves a useful purpose in PowerShell: it shows reflection information, namely the signatures (list of parameters and their types) of the method and its overloads; e.g.:

    OverloadDefinitions
    -------------------
    string ToUpper()
    string ToUpper(cultureinfo culture)
    
Sign up to request clarification or add additional context in comments.

Comments

2

You put a space between InputBox and ('Enter. Remove the space so it gets executed as a method with arguments:

$log = [Microsoft.VisualBasic.Interaction]::InputBox('Enter the Event ID you are looking for.', 'Event ID Query')

Otherwise what you are trying to tell PowerShell to do is return the method object itself, and it doesn't know what to do with the resulting subexpression in parentheses.

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.