小编use*_*904的帖子

如何使用PowerShell更改XML Element属性的值?

我试图从XML标记访问和更改特定属性

XML:

<office>
  <staff branch="Hanover" Type="sales">
    <employee>
        <Name>Tobias Weltner</Name>
        <function>management</function>
        <age>39</age>
    </employee>
    <employee>
        <Name>Cofi Heidecke</Name>
        <function>security</function>
        <age>4</age>
    </employee>
  </staff>
  <staff branch="London" Type="Technology">
   <employee>
    <Name>XXXX</Name>
    <function>gement</function>
    <age>39</age>
Run Code Online (Sandbox Code Playgroud)

从上面的例子我想打印分支属性,然后想要在所有整个XML中使用一个值(如纽约)更改它,并使用下面的代码来执行该操作

       $xml=New-Object XML

      $xml.Load("C:\FE6Work.xml")

      $node=$xml.SelectNodes("/office/staff")

      write-output $node.branch
      $node.branch="New York"
Run Code Online (Sandbox Code Playgroud)

但得到一个错误说明无法找到该元素.

有人可以帮忙吗?

xml powershell powershell-2.0

28
推荐指数
2
解决办法
4万
查看次数

如果文本文件包含字符串,则使用PowerShell从文本文件中删除行

我试图使用下面的PowerShell代码从包含部分字符串的文本文件中删除所有行:

 Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt
Run Code Online (Sandbox Code Playgroud)

实际的字符串是H|159|28-05-2005|508|xxx,它在文件中重复多次,我试图只匹配上面指定的第一部分.那是对的吗?目前我的输出是空的.

我错过了什么吗?

powershell powershell-2.0 powershell-3.0

26
推荐指数
4
解决办法
8万
查看次数

标签 统计

powershell ×2

powershell-2.0 ×2

powershell-3.0 ×1

xml ×1