Sheldon Lewis
Sheldon Lewis

Reputation: 35

Invoke-Sqlcmd : Conversion failed when converting date and/or time from character string

My PowerShell script keeps failing when trying to import a CSV file.

The error message reads: "Invoke-Sqlcmd : Conversion failed when converting date and/or time from character string."

In my CSV file, I have a column called "LastWriteTime"

Here is my PowerShell script

$database = 'test'
$server = 'leasesql'
$table = 'dbo.ssis'

Import-CSV C:\temp\text.csv | ForEach-Object {
Invoke-Sqlcmd -Database $database -ServerInstance $server -Query "INSERT INTO $table (PSComputerName, FullName, Extension, LastWriteTime)
 VALUES ('$_.PSComputerName','$_.FullName','$_.Extension','$_.LastWriteTime,')"

}

Upvotes: 0

Views: 1236

Answers (1)

Sheldon Lewis
Sheldon Lewis

Reputation: 35

I was able to find out that I need to put the columns in $($_.colmunname)

Import-CSV C:\temp\ssis.csv | ForEach-Object { Invoke-Sqlcmd -Database $database -ServerInstance $server -Query "INSERT INTO $table VALUES ('$($.PSComputerName)','$($.FullName)','$($.Extension)','$($.LastWriteTime)')"

Upvotes: 1

Related Questions