我遇到使用PowerShell从XML配置文件的XPath获取和设置属性值的问题。 属性就像
"<add key="TransactDeadLetterQueueThreshold" value="0" />".
示例XML文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ReportFile" value="C:\Program Files\Fusion\Viewer Suite Monitoring\ViewersMonitoring\monitor.txt" />
<add key="QueueThreshold" value="1000" />
<add key="TransactDeadLetterQueueThreshold" value="0" />
<add key="TimeCardsThreshold" value="6000" />
<add key="PercentDiskSpaceThreshold" value="10" />
<add key="QueueCreatedFormat" value="The {0} queue is created." />
<add key="QueueOverThresholdFormat" value="The {0} queue size is larger than {1} messages." />
<add key="QueueTransactDeadLetterOverThresholdFormat" value="The transactional dead queue size is larger than {0} messages." />
<add key="DriveThresholdFormat" value="The {0} drive space is less than {1}." />
<add key="QueueRemovedFormat" value="The {0} queue is removed." />
<add key="TimeCardsOverThresholdFormat" value="The time card records count is greater than or equal to {0} ." />
<add key="FailedConnectDbFormat" value="Failed: unable to access the database, reason: {0}" />
<add key="LogName" value="Viewer Suite Monitor" />
<add key="ViewersPrivateQueues" value="private$\adt,private$\infusionviewer,private$\timecards" />
<add key="DrivetobeMonitored" value="D:" />
</appSettings>
我试图通过以下方式获取属性:
$xdoc = Get-content "C:\Program Files\Fusion\Viewer Suite Monitoring\CareFusion.Infusion.ViewerSuite.Monitoring.exe.config"
$value = ($xdoc.configuration.appSettings.SelectSingleNode(add[@key = "PercentDiskSpaceThreshold"])) + $value.GetAttribute('key')
也尝试过这种方式:
$xdoc = Get-Content "C:\Program Files\Fusion\Viewer Suite Monitoring\Fusion.ViewerSuite.Monitoring.exe.config"
$value = $xdoc.selectSingleNode("/configuration/appSettings/add[@key = ""TransactDeadLetterQueueThreshold""] ")'+"
$value.GetAttribute('key')"
我收到错误。我不知道这有什么问题。 请帮忙。
答案 0 :(得分:0)
你走在正确的轨道上。
确保PowerShell将XML视为具有[xml]
类型加速器的实际XmlDocument对象:
$XmlPath = '.\path\to\app.config'
$XDoc = [xml](Get-Content $XmlPath)
选择要更改其值的节点:
$TransactDeadLetterNode = $XDoc.SelectSingleNode('//add[@key = "TransactDeadLetterQueueThreshold"]')
设置属性:
$TransactDeadLetterNode.SetAttribute("value",1)
(对要更改的任何其他节点重复)
最后保存文档:
$XDoc.Save($XmlPath)