【问题标题】:XML data coming in newline using invoke-sql command in powershell在 powershell 中使用 invoke-sql 命令进入换行符的 XML 数据
【发布时间】:2013-09-18 03:41:20
【问题描述】:

我正在使用一个 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

【问题讨论】:

    标签: sql-server xml powershell


    【解决方案1】:

    尝试使用 set-content 而不是 out-file。 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
    

    【讨论】:

    • out-string 也在截断数据
    • 不是字符串。请参阅我修改后的示例,将输出文件的输出与您的设置内容代码进行比较。
    • 我仍然得到换行符和额外的空格。我有大量的 XML 数据,这是个问题吗?
    • 源代码中可能有换行符。你可以通过join来运行它:
    • $xml = invoke-sqlcmd -inputfile ...; $xml = $xml -加入
    【解决方案2】:

    使用 Invoke-SQLCMD 命令的 -MaxCharLength 参数。默认为 4000。它将截断大的 XML。

    Invoke-SqlCmd doesn't return long string?

    【讨论】:

      猜你喜欢
      • 2022-07-07
      • 2020-03-17
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多