将数据导出到 CSV 时,根据条件连接 PowerShell 中的文本

Concatenate text in PowerShell based on a condition while exporting data to CSV

我在 AD 中有如下数据:

DisplayName TelephoneNumber
Alex        111-1111
John        222-2222
Peter       333-3333,2051

我有一个如下所示的 powershell 脚本:

Get-ADUser -SearchBase 'OU=Domain Users,DC=nxcc,DC=local' -Filter {(enabled -eq $true) -and (mail -like "*.*") -and (PhysicalDeliveryOfficeName -like "*")} -Properties displayname, telephonenumber | select displayname, telephonenumber |  Export-csv -NoTypeInformation -encoding UTF8 -Path "\myserver\c$\AllUserContactInfo.csv"

我想要做的是在 TelephoneNumber 字段的末尾添加 ,0000 如果该字段不包含逗号字符,同时导出为 CSV。所以预期的输出是:

DisplayName TelephoneNumber
Alex        111-1111,0000
John        222-2222,0000
Peter       333-3333,2051

我怎样才能做到这一点?感谢您的帮助。

此致。

而不是:

| select displayname, telephonenumber

使用 calculated property:

| Select-Object DisplayName,
    @{n='TelephoneNumber'; e={($_.TelephoneNumber.Split(',', 2) + '0000')[0,1] -Join ','}}

在表达式中,电话号码被拆分(使用逗号作为分隔符)。如果没有逗号,这将 return 只有一项,否则为两项。然后将 '0000' 添加到列表中(作为第二个或第三个项目),而不是只取前两个项目并用逗号再次加入它们。