使用XPATH代替查找具有SID属性的所有节点,如下所示:
$objs = @()
$nodes = $userfile.SelectNodes("//*[@SID]")
foreach ($node in $nodes) {
$sid = $node.attributes['SID'].value
$dispName = $node.attributes['DISPLAYNAME'].value
$obj = new-object psobject -prop @{SID=$sid;DISPNAME=$dispName}
$objs += $obj
}
$objs
这是输出的示例:
$xml = [xml]@"
"@
$objs = @()
$nodes = $xml.SelectNodes("//*[@SID]")
foreach ($node in $nodes) {
$sid = $node.attributes['SID'].value
$dispName = $node.attributes['DISPLAYNAME'].value
$obj = new-object psobject -prop @{SID=$sid;DISPNAME=$dispName}
$objs += $obj
}
$objs
输出:
SID DISPNAME
--- --------
foosid foodisp
barsid bardisp
blechsid blechdisp