在PowerShell脚本中,我尝试从以下XML文件中选择第二个MemberShipRule(用于UnixComponentGroup(:
<Discoveries>
<Discovery ID="Service_ARCHIBUS_SCPopulation" Enabled="true" Target="Service_ARCHIBUS" ConfirmDelivery="false" Remotable="true" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes />
<DataSource ID="DS" TypeID="SystemCenter!Microsoft.SystemCenter.GroupPopulator">
<RuleId>$MPElement$</RuleId>
<GroupInstanceId>$Target/Id$</GroupInstanceId>
<MembershipRules>
<MembershipRule>
<MonitoringClass>$MPElement[Name="WindowsComponentGroup_Service_ARCHIBUS"]$</MonitoringClass>
<RelationshipClass>$MPElement[Name="ATS.Application.ARCHIBUS.WindowsServiceHealthRollup"]$</RelationshipClass>
</MembershipRule>
<MembershipRule>
<MonitoringClass>$MPElement[Name="UnixComponentGroup_Service_ARCHIBUS"]$</MonitoringClass>
<RelationshipClass>$MPElement[Name="ATS.Application.ARCHIBUS.UnixServiceHealthRollup"]$</RelationshipClass>
</MembershipRule>
</MembershipRules>
</DataSource>
</Discovery>
</Discoveries>
字符串"ARCHIBUS"存储在变量$appnorm中。我尝试了不同的版本,我的最后一个是:
$xml = Get-Content $apppath
$appnorm = "ARCHIBUS"
$node = $xml.SelectSingleNode('//MembershipRule/MonitoringClass[.=$MPElement[Name="UnixComponentGroup_Service_' + $appnorm + '"]$]')
结果始终是null
值。如果有人能帮助我,那就太好了。
谢谢!
亲切问候
乌尔夫
那是因为你的$xml
对象是一个字符串。因此,它没有该方法(您可以将任何对象管道传输到Get-Member
以查看其类型和方法(。因此,您需要先将其转换为xml,然后可以对其调用SelectSingleNode
,但我会使用简化的PowerShell
语法搜索您的元素:
[xml]$xml = Get-Content $apppath
$appnorm = "ARCHIBUS"
$xml.Discoveries.Discovery.DataSource.MembershipRules.MembershipRule | Where-Object { $_.MonitoringClass -eq "`$MPElement[Name=`"WindowsComponentGroup_Service_$appnorm`"]`$" }