[英]Powershell Script using Invoke-SQL command,needed for SQL job, the SQL Server version of Powershell is somewhat crippled, is there a workaround?
[英]XML data coming in newline using invoke-sql command in powershell
我正在使用Powershell脚本,该脚本将从FOR XML EXPLICIT类型的sqlquery返回XML。 现在,查询在带有标题和虚线的列中返回了XML
XML 32776498797309
---------------------
<data>asdsafafaf<\data><value>dfsasfasfdfasdf....
dfdsfsdgregrge<\value><value>asdfasdfadfadfsda<\v
alue>
在这里,我们了解了一些如何删除标头,虚线和数据截断的方法。 但是数据仍然以新行显示,因此,如果我们打开XML,则会引发错误,因为在某些位置,标签已如上所示分布。 基本上,有换行问题。 我们尝试了-width 4096,但是由于XML很大,所以不合适。 请帮助,卡住很长时间。
使用的查询:
invoke-sqlcmd -inputFile $inputFilePath -serverinstance $dbHostName
-database $dbName -username $userName -password $password
| Format-Table -hide -Wrap -AutoSize
| Out-File -filePath $outputFilePath -width 4096
尝试使用set-content而不是out-file。 外文件将使用主机/控制台格式,而set-content不使用。 您需要处理将XML输出为字符串的情况
invoke-sqlcmd ....| select -expandproperty XML | set-content -path $outputFilePath
编辑。 添加了完整的工作示例 :
#Contents of my test inputfile.sql:
SELECT CAST ('<EVENT_INSTANCE>
<EventType>CREATE_TABLE</EventType>
<PostTime>2011-04-26T15:05:21.333</PostTime>
<SPID>56</SPID>
<ServerName>Z001\SQL1</ServerName>
<LoginName>Contoso\u00</LoginName>
<UserName>dbo</UserName>
<DatabaseName>AdventureWorksDW2008R2</DatabaseName>
<SchemaName>dbo</SchemaName>
<ObjectName>AdventureWorksDWBuildVersion</ObjectName>
<ObjectType>TABLE</ObjectType>
<TSQLCommand>
<SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" />
<CommandText>CREATE TABLE [dbo].[AdventureWorksDWBuildVersion] (
[DBVersion] [nvarchar] (50) NULL,
[VersionDate] [datetime] NULL
) ON [PRIMARY];
</CommandText>
</TSQLCommand>
</EVENT_INSTANCE>' AS XML)
$inputFilePath = "C:\Temp\inputfile.sql"
$dbHostName = "$env:computername\sql1"
$outputFilePath = "C:\Temp\output.txt"
#Notice Extra Line Breaks
invoke-sqlcmd -inputFile $inputFilePath -serverinstance $dbHostName |
Format-Table -hide -Wrap -AutoSize |
Out-File -filePath $outputFilePath -width 4096
#No Extra line breaks using set-content
invoke-sqlcmd -inputFile $inputFilePath -serverinstance $dbHostName | select -expandproperty Column1 | set-content -Path $outputFilePath
使用Invoke-SQLCMD命令的-MaxCharLength参数。 默认情况下为4000。它将截断大型XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.