简体   繁体   English

使用 xp_cmdshell 从 T-SQL 脚本执行 SQLPS 或 PowerShell 时遇到问题

[英]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.我最初创建了一组脚本来使用 SQLCMD 从动态查询系统中提取结果到 CSV。 However, due to large text fields in the datasets containing a large amount of formatting codes, the CSV files became unwieldly.然而,由于数据集中的大文本字段包含大量格式代码,CSV 文件变得笨拙。 I then switched to using SQLPS or PowerShell, which did a great job of formatting the output files.然后我切换到使用 SQLPS 或 PowerShell,它在格式化 output 文件方面做得很好。 Unfortunately, I am unable to get my extract CSV files to save when running the T-SQL file manually from SSMS.不幸的是,当从 SSMS 手动运行 T-SQL 文件时,我无法获取要保存的提取 CSV 文件。 I can paste the same output code into SQLPS and it works fine using PowerShell.exe or SQLPS.exe.我可以将相同的 output 代码粘贴到 SQLPS 中,使用 PowerShell.exe 或 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.我检查了提取文件夹的文件权限,尝试了 SQLPS 和 PowerShell 的不同选项,并查看了进程监视器中的执行情况,但我不知道为什么这不起作用。

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.我的要求是提取一个标准的 CSV 文件,不幸的是,我试图利用我在项目早期使用 SQLCMD 时拼凑的大量 T-SQL 代码。 It will also need to be an automated process, which is why I was trying to leverage PowerShell or SQLPS.它还需要是一个自动化过程,这就是我尝试利用 PowerShell 或 SQLPS 的原因。 I am using SQL Server 2019 on a dev machine running Windows 10.我在运行 Windows 10 的开发机器上使用 SQL Server 2019。

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好吧,在对我的疯狂过程进行了大量研究之后,我绝对应该将其重写为 PowerShell 脚本(而不是使用 xp_cmdshell),我找到了解决方案,即

  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' ');使用SET @Cmd = REPLACE(REPLACE(@Cmd, NCHAR(13), N''), NCHAR(10), N' ');

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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