编辑配置值Powershell

时间:2018-12-27 16:29:40

标签: xml powershell

我熟悉在Powershell中引用节点,但是由于每个值都没有唯一的标识符,因此我并不完全确定如何编辑以下节点。我可以基于路径编辑值吗?每个路径都引用一个SSIS变量。我只需要编辑configureValue,但这跨15个节点。以下是这些节点的2个示例:

<Configuration ConfiguredType="Property" Path="\Package.Variables[User::var1].Properties[Value]" 
    ValueType="String">
        <ConfiguredValue>SomeValue</ConfiguredValue>
 </Configuration>
 <Configuration ConfiguredType="Property" Path="\Package.Variables[User::var2].Properties[Value]" 
    ValueType="String">
        <ConfiguredValue>AnotherValue</ConfiguredValue>
</Configuration>

在上面的示例中,我希望基于Path编辑“ someValue”,这可能吗?我本来是按行号进行编辑的,但很快意识到每个客户端文件的行号都会发生变化。

理想情况下,我想遍历文件路径中的所有文件,并对每个单独的文件执行此操作。

已更新:附件是我的工作代码,谢谢@ gms0ulman!

$filePath = ("\\server1\test")  #location of all files

    Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){

      $xml = [xml](Get-Content $file.FullName)
      #updating attribute
      $node = $xml.root.Configuration
      ($xml.root.Configuration | Where-Object {$_.Path -like "*var1*"}).ConfiguredValue = "newValue"
      $xml.Save($file.FullName)
    }

1 个答案:

答案 0 :(得分:3)

可能:

# xml object to use in example
[xml]$xml = @"
<root>
    <Configuration ConfiguredType="Property" Path="\Package.Variables[User::var1].Properties[Value]" 
        ValueType="String">
            <ConfiguredValue>SomeValue</ConfiguredValue>
     </Configuration>
     <Configuration ConfiguredType="Property" Path="\Package.Variables[User::var2].Properties[Value]" 
        ValueType="String">
            <ConfiguredValue>AnotherValue</ConfiguredValue>
    </Configuration>
</root>
"@

# standard node dot-indexing, before
$xml.root.Configuration

# ConfiguredType Path                                             ValueType ConfiguredValue
# -------------- ----                                             --------- ---------------
# Property       \Package.Variables[User::var1].Properties[Value] String    SomeValue
# Property       \Package.Variables[User::var2].Properties[Value] String    AnotherValue


# change the value based on Path
($xml.root.Configuration | Where-Object {$_.Path -like "*var1*"}).ConfiguredValue = "newValue"


# standard node dot-indexing, after
$xml.root.Configuration

# ConfiguredType Path                                             ValueType ConfiguredValue
# -------------- ----                                             --------- ---------------
# Property       \Package.Variables[User::var1].Properties[Value] String    newValue
# Property       \Package.Variables[User::var2].Properties[Value] String    AnotherValue