简体   繁体   中英

Trouble executing SQLPS or PowerShell from T-SQL script using xp_cmdshell

I had originally created a set of scripts to extract the results from a system of dynamic queries to CSV using SQLCMD. However, due to large text fields in the datasets containing a large amount of formatting codes, the CSV files became unwieldly. I then switched to using SQLPS or PowerShell, which did a great job of formatting the output files. Unfortunately, I am unable to get my extract CSV files to save when running the T-SQL file manually from SSMS. I can paste the same output code into SQLPS and it works fine using PowerShell.exe or SQLPS.exe.

I checked file permissions on the extract folder, tried different options for SQLPS and PowerShell, and reviewed the execution in Process Monitor, but I cannot figure out why this will not work.

SET @Cmd = 'sqlps 
     $FromDate = "' + @FromDate + '";
     $ToDate = "' + @ToDate + '";
     $IncidentTypeName = "' + @IncidentTypeName + '";
     $DateField = "' + @DateField + '";
     $QueryType = "' + @QueryType + '";
     $PersonTypeID = "' + @PersonTypeID + '";
     $var = "FromDate=$FromDate", "ToDate=$ToDate", "IncidentTypeName=$IncidentTypeName", "DateField=$DateField", "QueryType=$QueryType", "PersonTypeID=$PersonTypeID";

invoke-sqlcmd -ServerInstance ' +@Server+ ' -Database ' +@Database+ ' -Username '+@Login+' -Password '+@Password + ' -QueryTimeout 0 ' +
    ' -InputFile "'+@SqlScriptFolder+@InputFileType+'" -Variable $var | export-csv -Delimiter "," -NoTypeInformation -Path "'+@ExtractFolder+@OutputFile+'.csv"'

print @Cmd
EXEC master..xp_cmdshell @Cmd

My requirements are to extract a standard CSV file, and unfortunately I am trying to leverage the large amount of T-SQL code I put together earlier in the project when working with SQLCMD. It will also need to be an automated process, which is why I was trying to leverage PowerShell or SQLPS. I am using SQL Server 2019 on a dev machine running Windows 10.

Any ideas on this one?

Well, after much research into my crazy process, which I should definitely rewrite to be a PowerShell script (and not use xp_cmdshell), I found the solution, which is to

  1. escape the " (double-quotes) with a \ (backslash)
  2. replace the carriage returns and newlines using SET @Cmd = REPLACE(REPLACE(@Cmd, NCHAR(13), N''), NCHAR(10), N' ');

from invoke-sqlcmd-doesnt-work-when-used-with-xp-cmdshell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM