在PowerShell中过滤/拆分输出

时间:2019-03-04 11:17:49

标签: powershell filtering

我正在尝试过滤/拆分从此代码获得的输出:

Get-ADUser -Filter * -Properties proxyaddresses |
    Where-Object {$_.proxyaddresses -Like "ixi*"} |
    Select sAMAccountName, proxyaddresses

在“ proxyaddresses”下,有一个以“ ixi:+49”开头的数字,以及一些我不需要的其他信息。我该如何操作此代码以仅获取该数字,然后以某种方式找到一种对其进行更多过滤以仅将最后四位数字作为输出的方法?

1 个答案:

答案 0 :(得分:1)

ProxyAddresses是一个数组,因此-Like对此无效。 如果我正确理解了这个问题(未测试),这可能会有所帮助

$re = [regex]'ixi:\+49.*(\d{4})$'
Get-ADUser -Filter * -Properties ProxyAddresses |
    Where-Object {$_.ProxyAddresses -match $re.toString()} |
    Select-Object SamAccountName, ProxyAddresses,
                  @{Name = 'ixi'; Expression = {$re.Match($_.ProxyAddresses).Groups[1].Value}}

正则表达式详细信息:

ixi:         Match the characters “ixi:” literally
\+           Match the character “+” literally
49           Match the characters “49” literally
.            Match any single character that is not a line break character
   *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(            Match the regular expression below and capture its match into backreference number 1
   \d        Match a single digit 0..9
      {4}    Exactly 4 times
)
$            Assert position at the end of the string (or before the line break at the end of the string, if any)