2013-08-28 25 views
1

我想要执行以下操作:选择Configuration节点,并根据ObjectName值更改ConfigurationString节点值。如何使用PowerShell更改DTSX xml文件中的多个节点

的XML如下:

<DTS:Executable xmlns:DTS="www.microsoft.com/.." DTS:ExecutableType="SSIS.Package"> 
    <DTS:Configuration> 
    <DTS:Property DTS:Name="ConfigurationType">1</DTS:Property> 
    <DTS:Property DTS:Name="ConfigurationString">change me</DTS:Property> 
    <DTS:Property DTS:Name="ObjectName">Configuration_1</DTS:Property> 
    <DTS:Property DTS:Name="DTSID">{..}</DTS:Property> 
    </DTS:Configuration> 
    <DTS:Configuration> 
    <DTS:Property DTS:Name="ConfigurationType">1</DTS:Property> 
    <DTS:Property DTS:Name="ConfigurationString">me to please</DTS:Property> 
    <DTS:Property DTS:Name="ObjectName">Configuration_2</DTS:Property> 
    <DTS:Property DTS:Name="DTSID">{..}</DTS:Property> 
    </DTS:Configuration> 

我有下面的代码改变ConfigurationString当有这种类型的节点只有一个实例。

$item = [xml](Get-Content -Path($item_path)) 
$item.Executable.Configuration.Property | ? { $_.name -eq 'configurationstring'} | % { $_.'#text' = "text" } 
$item.Save($item_path) 

我试图在? { $_.name -eq 'configurationstring'}添加一个条件,以检查是否ObjectName是所需的,但我不能让回Configuration节点,改变ConfigurationString节点值。

我已经使用SelectSingleNode方法也试过,但没有奏效:

$item.SelectSingleNode("Executable/Configuration/Property[@ObjectName='Configuration_1']") | ? { $_.name -eq 'configurationstring'} | % { $_.'#text' = "test" } 

感谢和问候。

回答

0

它使用SelectSingleNode获取父

$xml.Executable.Configuration | % { $_.property } | # Get all of the properties 
    ? { $_.name -eq "ObjectName" -and $_."#text" -eq "Configuration_1" } | #Get the one we are looking for 
    % { $_.selectSingleNode("..").Property } | # Get all of it's sibling properties 
    ? { $_.name -eq 'configurationstring'} | # Get the property we want to change 
    % { $_.'#text' = "text" }     # Update property 

一件简单的事情这也许不是最干净的,但它应该完成这项工作。

+0

谢谢你的回答,但问题仍然存在。如果我只用一个'DTS:Configuration'来使用你的代码,它就可以工作,但是当有+1'DTS:Configuration'时,它什么也不做。 – Bparra

+0

再试一次,它使用的是V3的功能,但您使用的是v2 –

+0

现在,它的工作原理,谢谢! – Bparra

0
PS C:\> $item.Executable.Configuration | % { $_.ChildNodes.GetEnumerator() } | ? {$_.Name -eq "ConfigurationString"} | % { $_.'#text' = "sometext"} 

$item.Save("C:\Scripts\so\dtsx.xml") 
相关问题