使用PowerShell

时间:2016-10-06 05:29:52

标签: xml powershell

我的XML文档如下

  <Batch>
  <Alter>
    <ObjectDefinition>
      <DataSources>
        <DataSource>
          <ConnectionString>Provider=SQLNCLI11.1;Data Source=ABC;Integrated Security=SSPI;Initial Catalog =TesDB1</ConnectionString>
          <ConnectionString>Provider=SQLNCLI11.1;Data Source=XYZ;Integrated Security=SSPI;Initial Catalog =TesDB1</ConnectionString>
        </DataSource>
      </DataSources>
    </ObjectDefinition>
  </Alter>
</Batch>

我想替换整个连接字符串节点,如下所示

$ConnectionString = "Provider=SQLNCLI11.1;Data Source=$DataSource;Integrated Security=SSPI;Initial Catalog =$Database"

这是我尝试过的,但这只是替换了一个节点内容

$XMLA = "D:\employee.xml"
$SqlDataBase = [xml](Get-Content $XMLA)
$data = $SqlDataBase.SelectNodes("Batch/Alter/ObjectDefinition/DataSources/DataSource/ConnectionString")
#$data
$DataSource = "localhost"
foreach($xn in $data)
{

   $DataBase = $xn.'#text'.Split(";").Split("=")
   $DataBase[$DataBase.Length - 1]
   $DataBase = $DataBase[$DataBase.Length - 1]

   $ConnectionString = "Provider=SQLNCLI11.1;Data Source=$DataSource;Integrated Security=SSPI;Initial Catalog =$Database"

   $SqlDataBase.SelectSingleNode("Batch/Alter/ObjectDefinition/DataSources/DataSource/ConnectionString").InnerText = "$ConnectionString";
}
$SqlDataBase.Save($XMLA)

所以更换多个节点的更好方法请告诉我

1 个答案:

答案 0 :(得分:0)

这是我从这里获取资源的最佳方式

Selecet XML Nodes by similar names in Powershell

mysqli