Christian Palacios
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: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : EndSquareBracketExpectedAt EndOfAttri bute
Thanks,
-Christian
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: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : EndSquareBracketExpectedAt
Thanks,
-Christian
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: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : MissingTypename
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: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : MissingTypename
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:
Then you need to enclose the complete column name in double or single quotes:
$_."[DEALERSHIP ADDRESS]" = $cityaddresses[$i]
ASKER
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: (:) [], SetValueInvocationExceptio n
+ FullyQualifiedErrorId : ExceptionWhenSetting
$StrQry = "SELECT [EXT COLOR], [DEALERSHIP ADDRESS]='' FROM ......"
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: (:) [], SetValueInvocationExceptio
+ FullyQualifiedErrorId : ExceptionWhenSetting
$StrQry = "SELECT [EXT COLOR], [DEALERSHIP ADDRESS]='' FROM ......"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! Putting it in just double quotes worked great. I thought I did try that before, but maybe I didn't. :)
$_.['DEALERSHIP ADDRESS'] = $cityaddresses[$i]