sql - Replace Single Quotes in PowerShell Or Excel CSV -
sql - Replace Single Quotes in PowerShell Or Excel CSV -
i'm wrapping script gets software versions , puts them csv, powershell sql. of software names have single quotes in them not allowed import sql. i'm trying find , replace these single quotes ticks @ point before final sql export. first instinct operate find , replace function in excel via powershell replace, i'm not sure how go or if best way.
any advice appreciated. i'm new this.
thanks
edit: advice far alroc. i'm working next piece:
##sql part## $csv = import-csv -path $updatepath\$($todaydate).csv import-module sqlps foreach ($item in $csv){ $csvappid = $($item.appid) $csvappname = $($item.appname) $csvversion = $($item.version) $sqlserver = "redacted" $sqldb = "redacted" $softwaretable = "redacted" invoke-sqlcmd -serverinstance "$sqlserver" -database "$sqldb" -query "insert dbo.ecca_sw_standard_2 (name, version, product_id) values (n'$csvappname', n'$csvversion' , n'$csvappid')" }
a few of variable values contain single quotes in strings, , powershell throws me error when gets these few. "invoke-sqlcmd : wrong syntax near 's'."
the reply create sub expressions in string replace '
''
.
invoke-sqlcmd -serverinstance "$sqlserver" -database "$sqldb" -query "insert dbo.ecca_sw_standard_2 (name, version, product_id) values (n'$($csvappname -replace "'", "''")', n'$($csvversion -replace "'", "''")' , n'$($csvappid -replace "'", "''")')"
this way when string expanded appropriately escaped sql insertion.
sql excel powershell csv
Comments
Post a Comment