Powershell: Conditionally changing objects in the pipeline

Posted by axk on Server Fault See other posts from Server Fault or by axk
Published on 2010-04-16T09:26:27Z Indexed on 2010/04/16 9:33 UTC
Read the original article Hit count: 242

Filed under:

I'm converting a CSV to SQL inserts and there's a null-able text column which I need to quote in case it is not NULL. I would write something like the following for the conversion:

Import-Csv data.csv | foreach { "INSERT INTO TABLE_NAME (COL1,COL2) VALUES ($($_.COL1),$($_.COL2));" >> inserts.sql }

But I can't figure out how to add an additional tier into the pipeline to look if COL2 is not equal to 'NULL' and to quote it in such cases. How do I achieve such behavior?

© Server Fault or respective owner

Related posts about powershell