V.Diesel
V.Diesel

Reputation: 77

Powershell "Import-Csv" with -Encoding does not work in SQL Server

I'm trying to set up a SQL Server Agent Job that imports some data from a CSV-document into a table using PowerShell.

The script I wrote works perfectly fine in Windows PowerShell ISE. As soon as I put it into the SQL Server Agent Job, it doesn't work any longer.

It seems like that the SQL Server does not accept the Argument -Encoding Default when importing the CSV-Document.

Code:

$Document = Import-CSV $path -Delimiter ";" -Header ... -Encoding Default 

Error Message:

A job step received an error at line 12 in a PowerShell script. The corresponding line is '$Document = Import-Csv $path -Delimiter ";" -Header ... -Encoding UTF8'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'A parameter cannot be found that matches parameter name 'Encoding'. '.

Any help is appreciated.

Upvotes: 1

Views: 2280

Answers (1)

Thomas Glaser
Thomas Glaser

Reputation: 2197

According to this article SQL Server 2012 launches PowerShell 2.0. For older versions it's even PowerShell 1.0.

The parameter -Encoding was introduced in PowerShell 3.0, so you will not be able to use it. I think you should be able to use ConvertFrom-Csv in combination with Get-Content instead:

Get-Content $path -Encoding Default | ConvertFrom-Csv -Delimiter ';'

Upvotes: 3

Related Questions