Alon Segal
Alon Segal

Reputation: 59

Trying to run a PowerShell script on version 2.0

I'm trying to run the statment on PowerShell 2.0:

$fixDepositsDataFile = Import-Csv F:\Balink\PowershellScripts\Wbalqry0.CSV -Header "1"
$fixDepositsDataFile | %{
    if ($_.1 -eq " ") {$_.1 = " "}
}

And I'm getting this exception:

enter image description here

I worked perfectly on 5.0, but the server that will run the script is 2.0.

Any way to avoid this issue?

Upvotes: 1

Views: 67

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Apparently you cannot use integers as property names in PowerShell v2. There are a number of ways to work around this issue. For instance you could put the property in an expression as @PetSerAl suggested ($_.(1)) or use a variable ($p = 1; ... $_.$p ...).

Personally I prefer putting the property name in quotes:

$fixDepositsDataFile | ForEach-Object {
    if ($_.'1' -eq " ") {$_.'1' = ""}
}

Quotes around property names also help mitigating other problematic scenarios, e.g. when a property name contains spaces.

Upvotes: 2

Related Questions