Link to home
Start Free TrialLog in
Avatar of Christian Palacios
Christian PalaciosFlag for Canada

asked on

PowerShell command to replace value before creating CSV file

Hi there,

I am creating multiple CSV files with PowerShell based on data from an SQL Server.  I am creating a blank column at first with the SQL Select command and putting this in the command, [DEALERSHIP ADDRESS]='', that way it has the right name I want, which is DEALERSHIP ADDRESS.  When I try to replace this value with a specific address from an array, I am getting this error below.  I'm thinking it's because the column name has two words.  How can I fix this replace command so that it recognizes the correct column and I am then able to update it before I create the CSV file?  Please ask any questions you might have, thanks!

At line:23 char:19
+     $_.[DEALERSHIP ADDRESS] = $cityaddresses[$i]
+                   ~
Missing ] at end of attribute or type literal.
At line:23 char:20
+     $_.[DEALERSHIP ADDRESS] = $cityaddresses[$i]
+                    ~~~~~~~~
Unexpected token 'ADDRESS]' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EndSquareBracketExpectedAtEndOfAttribute



Thanks,
-Christian
Avatar of sirbounty
sirbounty
Flag of United States of America image

Try enclosing it in quotes:

$_.['DEALERSHIP ADDRESS'] = $cityaddresses[$i]
Avatar of Christian Palacios

ASKER

Hi there,

Thank you, but it didn't work.

At line:45 char:9
+     $_.['DEALERSHIP ADDRESS'] = $cityaddresses[$i]
+         ~
Missing type name after '['.
At line:45 char:8
+     $_.['DEALERSHIP ADDRESS'] = $cityaddresses[$i]
+        ~
Missing property name after reference operator.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename
Avatar of oBdA
oBdA

So you're creating a column/property with the literal name "[DEALERSHIP ADDRESS]"?
Then you need to enclose the complete column name in double or single quotes:
$_."[DEALERSHIP ADDRESS]" = $cityaddresses[$i]

Open in new window

Thank you, I tried the double quotes around the entire name as you suggested, but now I'm getting this error.  I've also included part of my SQL string I'm using to retrieve the data.  What am I doing wrong?

Exception setting "[DEALERSHIP ADDRESS]": "The property '[DEALERSHIP ADDRESS]' cannot be found on this object. Verify that the property exists and can be set."
At line:45 char:5
+     $_."[DEALERSHIP ADDRESS]" = $cityaddresses[$i]
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting



$StrQry = "SELECT [EXT COLOR], [DEALERSHIP ADDRESS]='' FROM ......"
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  Putting it in just double quotes worked great.  I thought I did try that before, but maybe I didn't. :)