powershell invoke sqlcmd

I am trying to have powershell insert the contents of a file into a database table. the table is define as varchar(max)

the command, when run in SSMS, runs correctly. in powershell I am getting an error which I am assuming is due to the length of the contents (30,000 characters). the script runs fine on shorter texts. how can I get this to run?

$xml=get-content $file

$xml=([string]$xml).replace("'","''")

$cmd="INSERT INTO news.dbo.data_import(source_code,data,filename) VALUES (1,'$xml','$name' )"

invoke-sqlcmd $cmd

invoke-sqlcmd :

At C:\App_Vb\agencies\ap.ps1:9 char:4

+ invoke-sqlcmd $cmd

+ ~~~~~~~~~~~~~~~~~~

+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException

+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCo

mmand

February 5th, 2015 2:50am

Try:

invoke-sqlcmd -Query $cmd

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 6:53am

no change
February 5th, 2015 6:55am

Print the $cmd variable.  I'm a PowerShell noobie, but I think that you just put $cmd on the line.  Paste the string into sql server management studio and see if it runs.

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 7:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics