如何从生产线上提取用户

时间:2019-06-17 19:59:34

标签: string powershell parsing

我需要从与此类似的行中提取用户名。用户名是可变的,因此我想必须用正则表达式来攻击它。 (我对如何实现这一点一无所知)。

<RowGroup>
<RowGroup>
<RowTotal RowNumber="0">USER1</RowTotal>
<RowTotal RowNumber="1">USER2</RowTotal>
<RowTotal RowNumber="2">USER3</RowTotal>
<RowTotal RowNumber="3">USER4</RowTotal>
<RowTotal RowNumber="4">USER 5</RowTotal>
</RowGroup>
</RowGroup>
</RowGroups>

1 个答案:

答案 0 :(得分:2)

查看适用于PowerShell和XML的答案,例如How to iterate through XML in Powershell?

还要研究类似下面的代码

$data = @'
<RowGroups>
<RowGroup>
<RowGroup>
<RowTotal RowNumber="0">USER1</RowTotal>
<RowTotal RowNumber="1">USER2</RowTotal>
<RowTotal RowNumber="2">USER3</RowTotal>
<RowTotal RowNumber="3">USER4</RowTotal>
<RowTotal RowNumber="4">USER 5</RowTotal>
</RowGroup>
</RowGroup>
</RowGroups>
'@


$xml = [xml]$data

foreach ($row in $xml.RowGroups.RowGroup.RowGroup.RowTotal)
{
    $row.'#text'
}