如何使用powershell

时间:2015-06-30 12:08:10

标签: sql-server powershell ssis xml-namespaces

我的目的是使用模板在powershell SQL Server维护计划创建/修改中编写脚本。这些模板是使用dtutil提取到.dtsx文件的维护计划。

维护计划包含多项任务。

我希望能够为我的dtsx文件中的每个任务添加一个新数据库,以便导入它(替换SQL Server中的旧任务)。

我的dtsx文件示例:

<?xml version="1.0"?>
<DTS:Executable xmlns:DTS="www.microsoft.com/SqlServer/Dts"
  DTS:refId="Package"
  DTS:CreationDate="6/26/2015 11:24:25 AM"
  DTS:CreationName="Microsoft.Package"
  .....
  <DTS:Executables>
    <DTS:Executable
      DTS:refId="Package\All"
      DTS:CreationName="STOCK:SEQUENCE"
      DTS:Description="Subplan_1"
      DTS:Disabled="True"
      .....
      <DTS:Executables>
        <DTS:Executable
          DTS:refId="Package\All\Back Up Database Task"
          DTS:CreationName="Microsoft.DbMaintenanceBackupTask"
          DTS:Description="Back Up Database Task"
          DTS:DTSID="{04209de7-5ea4-4e5c-843e-451db1393625}"
          DTS:ExecutableType="Microsoft.DbMaintenanceBackupTask"
          DTS:LocaleID="-1"
          DTS:ObjectName="Back Up Database Task"
          DTS:TaskContact="Back Up Database Task; Microsoft Corporation; Microsoft SQL Server v9; © 2004 Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1"
          DTS:ThreadHint="0">
          .....
          <DTS:ObjectData>
            <SQLTask:SqlTaskData
              SQLTask:Connection="{6CAC94AA-3395-44B3-A935-D14FC08186DF}"
              SQLTask:DatabaseSelectionType="4"
              SQLTask:ServerVersion="12"
              SQLTask:ExtendedLogging="False"
              SQLTask:LocalConnectionForLogging="Local server connection"
              SQLTask:TaskName=""
              SQLTask:IgnoreDatabasesInNotOnlineState="False"
              SQLTask:BackupAction="0"
              .....
              SQLTask:BackupEncryptorName="" xmlns:SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask">
              <SQLTask:SelectedDatabases
                SQLTask:DatabaseName="db1" />
            </SQLTask:SqlTaskData>
          </DTS:ObjectData>
        </DTS:Executable>
        <DTS:Executable
          DTS:refId="Package\All\Check Database Integrity Task"
          DTS:CreationName="Microsoft.DbMaintenanceCheckIntegrityTask"
          .....
        ....
    ........

所以我试图在SQLTask中添加一个新项:SqlTask​​Data部分

              <SQLTask:SelectedDatabases
                SQLTask:DatabaseName="db2" />
            </SQLTask:SqlTaskData>

并且,使用这里的提示http://www.pawlowski.cz/2012/06/manipulating-ssis-packages-powershell/看起来非常好。

但是在这个技巧中,命名空间是uniq:DTS。

在我的文件中,我们有2个名称空间:DTS和SQLTask。

在尝试添加新部分之前,我只想阅读它。这是我的代码

#Get List of all relevat SSIS Packages
$file = Get-ChildItem "F:\maint_plan2014\modified\MaintenancePlan db.dtsx"

#get the content of SSIS package as XML
$dts = [xml](Get-Content $file.FullName)

#create XmlNamespaceManager
$mng = [System.Xml.XmlNamespaceManager]($dts.NameTable)
#add a DTS namespace to the XmlNamespaceManager
$mng.AddNamespace("DTS", "www.microsoft.com/SqlServer/Dts")
#add a SQLTask namespace to the XmlNamespaceManager
$mng.AddNamespace("SQLTask", "www.microsoft.com/SqlServer/Dts/Tasks/SQLTask")

#use XPath query to get all tasks
$tasks = $dts.SelectSingleNode("/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable", $mng)

直到这里,一切正常,$ tasks包含我的所有任务

现在,我尝试查询任务:

PS P:\> $dts.SelectSingleNode("/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable[1]/DTS:ObjectData", $mng)

SqlTaskData
-----------
SqlTaskData

PS P:\>

但是当我想更进一步时,在命名空间SQLTask中,结果为空:

PS P:\> $dts.SelectSingleNode("/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable[1]/DTS:ObjectData/SQLTask:SqlTaskData", $mng)
PS P:\>

当我这样查询时(见下文),它工作正常,但为了添加新数据库,我的意思是我必须使用命名空间,不是吗?

PS P:\> $dts.SelectSingleNode("/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable[1]/DTS:ObjectData", $mng).SqlTaskData


Connection                      : {6CAC94AA-3395-44B3-A935-D14FC08186DF}
DatabaseSelectionType           : 4
ServerVersion                   : 12
ExtendedLogging                 : False
LocalConnectionForLogging       : Local server connection
TaskName                        :
IgnoreDatabasesInNotOnlineState : False
BackupAction                    : 0
BackupIsIncremental             : False
BackupFileGroupsFiles           :
BackupDeviceType                : 2
BackupPhysicalDestinationType   : 2
BackupDestinationType           : 0
BackupDestinationAutoFolderPath : C:\ClusterStorage\Backup\MSSQL12.INSTDB\MSSQL\Backup
BackupActionForExistingBackups  : 0
BackupCreateSubFolder           : True
BackupFileExtension             :
BackupVerifyIntegrity           : True
ExpireDate                      : 2015-07-10T00:00:00
RetainDays                      : 14
InDays                          : True
UseExpiration                   : False
BackupCompressionAction         : 0
CopyOnlyBackup                  : False
IgnoreReplicaType               : False
CredentialName                  :
ContainerName                   :
UrlPrefix                       :
IsBackupEncrypted               : False
BackupEncryptionAlgorithm       : 0
BackupEncryptorType             : 0
BackupEncryptorName             :
SQLTask                         : www.microsoft.com/sqlserver/dts/tasks/sqltask
SelectedDatabases               : SelectedDatabases



PS P:\> $dts.SelectSingleNode("/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable[1]/DTS:ObjectData", $mng).SqlTaskData.SelectedDatabases

DatabaseName
------------
db1


PS P:\>

有人能帮帮我吗?也许我误解了管理xml文件和名称空间......

0 个答案:

没有答案