2013-08-28 20 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") 
相關問題