Powershell 3 questions
Posted on 2013-12-17
Here's a short sample to show the issue I'm experiencing with PS 4.0
I don't understand the logic of what is happening.
My background is BASIC.
I run a SQL query and then expect to be able to change the data on the current record being processed, which I take for granted is assigned to $CSVRecord by the FOREACH statement. However, it seems that the results are illogical, so please explain what is happening here.
My goal is to process a bunch of database records, change some of the fields in each record, duplicate some of the records by appending them to the end of the $CSV, and then finally exporting.
I have a 500 line program written and everything runs without errors, but the results of appended records are not what is expected.
I can do the same thing on the command line and it seems to work as I expected it to and I verify by using the index of $CSV to display the entire record.
For example, the record appended in the program is just a copy of the original record in $CSV, instead of being a modified $CSVRecord, but on the command line I can then modify a field manually (as done in the program) and append it correctly. I can then use something like $CSV[$CSV.count-1] to view the last appended record and it is correct.
What is going on?
# requires powershell 3+
$Query = "SELECT 'one', 'two', 'three' "
$CSV = Invoke-Sqlcmd -Query $Query
$CSV = @($CSV) + @($CSV)
write-host Show two idnetical records
write-host Why does this NOT show 2 unique records???
foreach ($CSVRecord in $CSV)
#loop through each record in $CSV and modify the 2nd column to be unique
$CSVRecord.Column2 = "RECORD #"+$i
write-host NOW WHAT is happening???
$CSVRecord.Column3 = "BUTTER"