我有两个针对 xml 文件的单独查询,当我单独运行它们时,它们工作正常。但是,当我在同一个脚本中运行它们时,第二个查询的输出不正确。
输入文件如下所示:
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<Safex>
<Add>
<User folder="/" name="D2\\[email protected]">
<GroupMembership>ADMINS</GroupMembership>
<GroupMembership>WebService</GroupMembership>
</User>
<User folder="/" name="D2\\[email protected]">
<GroupMembership>WebService</GroupMembership>
</User>
<User folder="/" name="D2\\[email protected]">
<GroupMembership>WebService</GroupMembership>
</User>
<User folder="/" name="D2\\[email protected]">
<GroupMembership>ADMINS</GroupMembership>
</User>
<User folder="/" name="D2\\[email protected]">
<GroupMembership>WebService</GroupMembership>
</User>
</Add>
</Safex>
我的脚本如下:
[xml]$xml = Get-Content $InputPath\test002.xml
$a = $xml.Safex.Add.User | ForEach-Object {
[PSCustomObject]@{
'Name' = $_.name
'Group' = $_.GroupMembership
}
}
$a
$b = $xml.Safex.Add.User | Where-Object { $_.GroupMembership -eq 'WebService' } |
Select-Object @{Name = 'Name'; Expression = {$_.Name}}, GroupMembership
#$b
当我注释掉 $b 时,$a 将返回以下内容:
Name Group
---- -----
D2\\[email protected] {ADMINS, WebService}
D2\\[email protected] WebService
D2\\[email protected] WebService
D2\\[email protected] ADMINS
D2\\[email protected] WebService
当我注释掉 $a 时,$b 将返回以下内容:
Name GroupMembership
---- ---------------
D2\\[email protected] {ADMINS, WebService}
D2\\[email protected] WebService
D2\\[email protected] WebService
D2\\[email protected] WebService
当我取消注释 $a 和 $b 时,我得到以下结果:
Name Group
---- -----
D2\\[email protected] {ADMINS, WebService}
D2\\[email protected] WebService
D2\\[email protected] WebService
D2\\[email protected] ADMINS
D2\\[email protected] WebService
D2\\[email protected]
D2\\[email protected]
D2\\[email protected]
D2\\[email protected]
我似乎无法弄清楚为什么当 $a 和 $b 都被取消注释时,$b 的标题不会出现,并且 GroupMembership 值也不会出现。
感谢您提供的任何帮助。