使用 Powershell 编辑 XML 属性 [英] Editing XML Attributes using Powershell

查看:84
本文介绍了使用 Powershell 编辑 XML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 .exe.config 文件,我试图在其中搜索特定属性,然后在 Windows 7 中使用 Powershell 4.0 版对其进行编辑,但我遇到了问题.我尝试了几件事,但没有任何成功.这是我正在使用的配置文件的精简版.

<预><代码><配置><配置1><section name="text" type="text, text, text=text" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="true"/></Config1><配置2><module debugLogLevel="Debug" Version="1.0.0.0"/><Interested-Item attribute-1="text-text" attribute2="0"></感兴趣的项目><模块><add name="something1"/><add name="something2"/><add name="something3"/><add name="something4"/><add name="something5"/><add name="something6"/><add name="something7"/></模块></Config2></配置>

我将如何使用 Powershell 更改感兴趣的项目下的属性 1?任何帮助将不胜感激.

以下是一些我尝试失败的例子.

$File = Get-Content $FileLocation$XML = [XML]$文件foreach ($XML.Config2.Interested-Item 中的 $attribute){$attribute = Interested-Item.attribute-1 = "更新短信"}XML.Save($FileLocation)

这对我没有任何作用.它根本不编辑文件.

$File = Get-Content $FileLocation$node = $File.SelectSingleNode("/Config2/Interested-Item[@attribute-1]")$node.Value = "新值"$File.Save($FileLocation)

这将返回以下错误.

在此对象上找不到属性Value".验证该属性是否存在并且可以设置.在 line:5 char:1+ $node.Value = "新值"+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidOperation: (:) [], RuntimeException+ FullQualifiedErrorId:PropertyNotFound

我尝试使用 Get-Help Select-XML 中的 -Xpath 来实现它,但也没有成功.

我唯一成功但在实践中不起作用的是以下内容.

(Get-Content $FileLocation) |ForEach-Object{$_ -replace "text-*", "NewText"} |设置内容 $FileLocation

这会在第一次强制工作,然后由于设置了新值而无法更新参数.我的目的是多次运行此脚本以更新一组配置文件.

解决方案

有很多方法.例如,您可以使用 XPath:

$File = Get-Content $FileLocation$XML = [XML]$文件$XPpath = "/configuration/Config2/Interested-Item[@attribute-1]"# 选择与我们的 $XPath 匹配的所有节点(即所有# '/configuration/Config2/Interested-Item' 具有属性的节点#'属性-1'.$nodes = $XML.SelectNodes($XPpath)# 更新所有选定节点的属性值.$nodes |% { $_.SetAttribute("attribute-1", "foo") }$XML.OuterXml |外文件 $FileLocation

更多信息这里 通常,w3schools.com 是您的朋友处理 HTML 或 XML.

So I have an .exe.config file that I am trying to search for a specific attribute within, and then edit it using Powershell Version 4.0 in Windows 7, and I am having issues. I have tried several things, and am not having any success. Here is a trimmed down version of the config file that I am using.

<configuration>
  <Config1>
    <section name="text" type="text, text, text=text" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="true" />
  </Config1>
  <Config2>
    <module debugLogLevel="Debug" Version="1.0.0.0" />
    <Interested-Item attribute-1="text-text" attribute2="0">
    </Interested-Item>
    <modules>
      <add name="something1" />
      <add name="something2" />
      <add name="something3" />
      <add name="something4" />
      <add name="something5" />
      <add name="something6" />
      <add name="something7" />
    </modules>
  </Config2>        
</configuration>

How would I go about changing the attribute-1 under Interested-Item, using Powershell? Any help would be greatly appreciated.

Here are a few examples of things I have unsuccessfully attempted.

$File = Get-Content $FileLocation
$XML = [XML]$File

foreach ($attribute in $XML.Config2.Interested-Item)
{
     $attribute = Interested-Item.attribute-1 = "Updated Texted"
}
XML.Save($FileLocation)

This does nothing for me. It doesn't edit the File at all.

$File = Get-Content $FileLocation
$node = $File.SelectSingleNode("/Config2/Interetested-Item[@attribute-1]")
$node.Value = "New-Value"
$File.Save($FileLocation)

This returns the following error.

The property 'Value' cannot be found on this object. Verify that the property exists and can be set.At line:5 char:1
+ $node.Value = "New-Value"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

I tried to implement it using -Xpath's from Get-Help Select-XML and was also unsuccessful with that as well.

The only thing that I have had any success with, which won't work in practice is the following.

(Get-Content $FileLocation) | ForEach-Object{$_ -replace "text-*", "NewText"} | Set-Content $FileLocation

This will forcibly work for the first time, and then afterwards won't be able to update the parameter, due to setting a new value. My intention is to run this script multiple times in order to update a group of config files.

解决方案

There are many ways. For example, you could use XPath:

$File = Get-Content $FileLocation
$XML = [XML]$File

$XPpath = "/configuration/Config2/Interested-Item[@attribute-1]"

# Selecting all nodes that match our $XPath (i.e. all
# '/configuration/Config2/Interested-Item' nodes that have attribute 
# 'attribute-1'.
$nodes = $XML.SelectNodes($XPpath)

# Updating the attribute value for all selected nodes.
$nodes | % { $_.SetAttribute("attribute-1", "foo") }

$XML.OuterXml | Out-File $FileLocation

More info here and generally w3schools.com is your friend when you're dealing with HTML or XML.

这篇关于使用 Powershell 编辑 XML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆