使用powershell编辑XML
好吧,我觉得自己像个大白痴。我在Powershell工作了一段时间,更多的是为了工作中的管理目的。也就是说,脚本不是我的强项 现在,我正试图编写一个PS脚本,在一堆机器上向XML添加一个部分,以添加设置来解决我们在某个应用程序中遇到的问题 XML文件如下所示使用powershell编辑XML,xml,powershell,Xml,Powershell,好吧,我觉得自己像个大白痴。我在Powershell工作了一段时间,更多的是为了工作中的管理目的。也就是说,脚本不是我的强项 现在,我正试图编写一个PS脚本,在一堆机器上向XML添加一个部分,以添加设置来解决我们在某个应用程序中遇到的问题 XML文件如下所示 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <add Name="testdata
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<add Name="testdata"/>
</configSections>
<connectionStrings>
<add Name="testdata"/>
</connectionStrings>
<ProvidersConfiguration>
<Providers>
<add Name="testdata"/>
</Providers>
</ProvidersConfiguration>
<FacadeSettings>
<Providers>
<add Name="testdata"/>
</Providers>
</FacadeSettings>
</configuration>
现在,我已经在谷歌上搜索了好几个小时,但有些东西我没有找到。因为我可以在脚本中加载文件,浏览所有设置,甚至修改现有值,但这不是我需要做的
我需要添加一个这样的部分
<NewSettings>
<add Name="setting"/>
</NewSettings>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<add Name="testdata"/>
</configSections>
<connectionStrings>
<add Name="testdata"/>
</connectionStrings>
<ProvidersConfiguration>
<Providers>
<add Name="testdata"/>
</Providers>
</ProvidersConfiguration>
<FacadeSettings>
<Providers>
<add Name="testdata"/>
</Providers>
</FacadeSettings>
<NewSettings>
<add Name="setting"/>
</NewSettings>
</configuration>
因此,我的配置文件如下所示
<NewSettings>
<add Name="setting"/>
</NewSettings>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<add Name="testdata"/>
</configSections>
<connectionStrings>
<add Name="testdata"/>
</connectionStrings>
<ProvidersConfiguration>
<Providers>
<add Name="testdata"/>
</Providers>
</ProvidersConfiguration>
<FacadeSettings>
<Providers>
<add Name="testdata"/>
</Providers>
</FacadeSettings>
<NewSettings>
<add Name="setting"/>
</NewSettings>
</configuration>
这是我搞不清楚的新闻设置部分,我确信当我绕着它转的时候,我会说“哦…”,但现在我的头撞到了墙上,需要一些帮助
# Create xml doc - assumes your xml is file E:\Scratch\test.xml
# If it's already in a variable, use $xml = [xml]$myVariable
$xml = [xml](Get-Content E:\Scratch\test.xml)
# Create new element for <NewSettings>
$newSettings = $xml.CreateElement("NewSettings")
# Create new element for <add>
$add = $xml.CreateElement("add")
# Create attribute "Name", and set its value
$settingsAttribute = $xml.CreateAttribute("Name")
$settingsAttribute.Value = "setting"
# Add attribute to <add>
$add.Attributes.Append($settingsAttribute)
# Add <add> to <NewSettings>
$newSettings.AppendChild($add)
# Add <NewSettings> to <configuration>
$xml.configuration.AppendChild($newSettings)
# Save to file
$xml.Save("E:\Scratch\new.xml")
#创建xml文档-假设您的xml是文件E:\Scratch\test.xml
#如果它已经在变量中,请使用$xml=[xml]$myVariable
$xml=[xml](获取内容E:\Scratch\test.xml)
#为创建新元素
$newSettings=$xml.CreateElement(“newSettings”)
#为创建新元素
$add=$xml.CreateElement(“添加”)
#创建属性“Name”,并设置其值
$settingsAttribute=$xml.CreateAttribute(“名称”)
$settingsAttribute.Value=“设置”
#添加属性到
$add.Attributes.Append($settingsAttribute)
#加上
$newSettings.AppendChild($add)
#加上
$xml.configuration.AppendChild($newSettings)
#保存到文件
$xml.Save(“E:\Scratch\new.xml”)
啊……所以我想我是想倒着做。如果我理解了这一点,我们将自己创建XML表,然后将其附加到现有XML中,我从一开始就试图将其添加到现有XML中,尝试向上(向下?)树。好的,在一些编辑之后尝试使用它,使它更符合我的需要,并且工作正常。我在开始尝试创建元素时走的是正确的道路,但没有完全掌握附加元素的部分。我知道我错过了一些东西,现在我明白了。非常感谢,这是一份可能的副本