BT123
BT123

Reputation: 23

Unexpected token error in powershell, executes fine in PS3+ having an issue on 2

Im working on a script to query some data from a csv in powershell and its working fine on machines with Powershell 3+ but we have clients with some machines that have 2.0 and I am trying to get it compatible.

This particular line of code is causing the error:

    $nameCheck += Import-Csv C:\temp\tokens.csv | Where-Object {$_. 'Customer Name' -clike "*$cid*"} | Select-Object 'Customer Name'

The error its giving is:

Unexpected token '.' in expression or statement. At C:\temp\tokenQueryv2.ps1:29 char:64 + $nameCheck += Import-Csv C:\temp\tokens.csv | Where-Object {$_. <<<< 'Customer Name' -clike "$cid"} | Select-Object 'Customer Name' + CategoryInfo : ParserError: (.:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

Based on the char position in the error it looks like it dosent like the {$_. '...' after the Where-Object I am assuming there is some different needed syntax for this on PS2.0 I am just struggling to find it in any online resources. If anyone knows or has any experience with something like this any input is much welcomed.

Thanks!

Upvotes: 0

Views: 371

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28963

Change:

Where-Object {$_. 'Customer Name' -clike "*$cid*"}

to

Where-Object {$_.'Customer Name' -clike "*$cid*"}

I'm surprised that PSv3 does support a space after the .

Upvotes: 1

Related Questions