簡體   English   中英

如何使用PowerShell從SSIS包文件中提取值?

[英]How do I extract a value from SSIS package file using PowerShell?

Sql Server Integration Services軟件包以dtsx擴展名存儲為xml文件。 我需要能夠使用Powershell提取其內部版本號。 文件頂部的相關部分如下所示:

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts">
<DTS:ExecutableType="MSDTS.Package.1">
<DTS:Property DTS:Name="PackageFormatVersion">2</DTS:Property>
<DTS:Property DTS:Name="VersionMajor">1</DTS:Property>
<DTS:Property DTS:Name="VersionMinor">0</DTS:Property>
<DTS:Property DTS:Name="VersionBuild">265</DTS:Property>

我需要提取VersionBuild號,但是DTS命名空間使我感到困惑。 獲得內容后,如何獲得價值?

上面的似乎不是有效的XML,但假設是有效的XML,則可以在PowerShell 2.0中使用Select-Xml cmdlet來執行此操作:

$ns = @{ dts = 'www.microsoft.com/SqlServer/Dts' }
$xpath = '//dts:Property[@dts:Name="VersionBuild"]'
[xml](get-content file.xml) | Select-Xml $xpath -Namespace $ns | %{$_.Node}

Name                                  #text
----                                  -----
VersionBuild                          265 

下面是一個提取所有SQL語句的示例,但是可以通過更改SelectNodes命令的xpath輕松地將其提取出諸如變量或連接之類的任何元素:

$package = [xml](Get-Content 'package.dtsx')
$ns = [System.Xml.XmlNamespaceManager]($package.NameTable)
$ns.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts")

$nodes = $package.SelectNodes("//property[@name = 'SqlCommand']", $ns)

$sqls = $nodes | % {

   New-Object PSObject -Property @{
        sql = $_.'#text'
        name = $_.ParentNode.parentNode.name
   }
}

$sqls

一旦文件是有效的XML(請參閱我的評論),您只需加載它並轉換為XML:

$x = [xml] (gc file.xml)

然后,您可以使用類似於屬性的符號來訪問節點:

$x.Executable.Property | Where-Object { $_.Name -like 'Version*' }

Name                                                        #text
----                                                        -----
VersionMajor                                                1
VersionMinor                                                0
VersionBuild                                                265

作為SQL Server Powershell Extensions CodePlex項目的一部分,有一個SSIS模塊。 您可以在其中執行以下操作:

import-module SSIS
$package = Get-ISPackage -path "C:\Program Files\Microsoft SQL Server\100\DTS\Packages\sqlpsx1.dtsx"
$package.VersionBuild

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM