Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell编辑XML属性_Powershell - Fatal编程技术网

使用Powershell编辑XML属性

使用Powershell编辑XML属性,powershell,Powershell,因此,我有一个.exe.config文件,我试图在其中搜索特定属性,然后在Windows 7中使用Powershell 4.0版对其进行编辑,但我遇到了问题。我试过几件事,但都没有成功。下面是我正在使用的配置文件的精简版本 <configuration> <Config1> <section name="text" type="text, text, text=text" allowLocation="true" allowDefinition="Eve

因此,我有一个.exe.config文件,我试图在其中搜索特定属性,然后在Windows 7中使用Powershell 4.0版对其进行编辑,但我遇到了问题。我试过几件事,但都没有成功。下面是我正在使用的配置文件的精简版本

<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>
这对我没什么用。它根本不编辑文件

$File = Get-Content $FileLocation
$node = $File.SelectSingleNode("/Config2/Interetested-Item[@attribute-1]")
$node.Value = "New-Value"
$File.Save($FileLocation)
这将返回以下错误

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
我尝试使用Get Help Select XML中的-Xpath实现它,但也没有成功

我唯一成功的,在实践中不起作用的是以下几点

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

这将在第一次强制工作,之后由于设置了新值,将无法更新参数。我的意图是多次运行此脚本以更新一组配置文件

有很多方法。例如,您可以使用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

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

如果您展示了您尝试过的代码,可能有人会指出哪里出了问题。我想这会更有帮助。非常感谢,这确实有用!我所做的唯一更改是将$XML.OUterXML | Out文件$FileLocation更改为$XML.Save($FileLocation),只是为了在查看文件以验证更改时保留文件的格式。周五和今天早上,我一整天都在为这件事苦苦挣扎,所以谢谢你的帮助!
$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