使用PowerShell将AD中的电话号码拆分为单独的列



我正在使用下面的powershell脚本:

Get-ADUser -SearchBase "OU=Domain Users,DC=nwvp,DC=local" -filter * -properties TelephoneNumber,DisplayName,mobile | select DisplayName,TelephoneNumber,mobile

,它给了我这样的输出:

DisplayName               TelephoneNumber         mobile               
-----------               ---------------         ------               
Paul Strong               +1 (902) 444-4444,6405  +1 (506) 111-1111    
Katie White               +1 (416) 333-3333,2204  +1 (905) 222-2222

我正在寻找更新输出的格式,如下所示:

LastName FirstName InternationalCode AreaCode PhoneNumber Extension DeviceType
Strong   Paul      1                 902      4444444     6405      Work
Strong   Paul      1                 506      1111111               Mobile
White    Katie     1                 416      3333333     2204      Work
White    Katie     1                 905      2222222               Mobile

如何格式化原始输出以显示所需的输出?

谢谢!

检查号码是否存在,如果存在则创建所需对象

Get-ADUser -SearchBase 'OU=Domain Users,DC=nwvp,DC=local' -Filter * -Properties TelephoneNumber, DisplayName, mobile | 
ForEach-Object {
if ($_.TelephoneNumber) {
[PSCustomObject]@{
LastName          = ($_.DisplayName.Split(' '))[1]
FirstName         = ($_.DisplayName.Split(' '))[0]
InternationalCode = if ($_.TelephoneNumber -match '+(d+)') { $Matches[1] }
AreaCode          = if ($_.TelephoneNumber -match '((d{3}))') { $Matches[1] }
PhoneNumber       = if ($_.TelephoneNumber -match '[d-]{7,}') { $Matches[0] -replace 's|-' }
Extension         = if ($_.TelephoneNumber -match ',(d+)') { $Matches[1] }
DeviceType        = 'Work'
}
}
if ($_.Mobile) {
[PSCustomObject]@{
LastName          = ($_.DisplayName.Split(' '))[1]
FirstName         = ($_.DisplayName.Split(' '))[0]
InternationalCode = if ($_.Mobile -match '+(d+)') { $Matches[1] }
AreaCode          = if ($_.Mobile -match '((d{3}))') { $Matches[1] }
PhoneNumber       = if ($_.Mobile -match '[d-]{7,}') { $Matches[0] -replace 's|-' }
Extension         = ''
DeviceType        = 'Mobile'
}
}
}

我认为您可以使用PowerShell名称,表达式来定制所需格式的输出首先将输出存储到数组(我认为AD查询给出不同数据类型的输出,因此最好明确定义数组的数据类型)

[array]$myArray=Get-ADUser -SearchBase "OU=Domain Users,DC=nwvp,DC=local" -filter * -properties TelephoneNumber,DisplayName,mobile | select DisplayName,TelephoneNumber,mobile

然后用n &E自定义如下

$myArray|select @{n="LastName";e={([string]($_.DisplayName)).Split(" ")[1]}},
@{n="FirstName";e={([string]($_.DisplayName)).Split(" ")[0]}},
@{n="InternationalCode";e={([string]($_.TelephoneNumber)).Split(" ")[0]}},
@{n="AreaCode";e={(([string]($_.TelephoneNumber)).Split(")")[0]).Split("(")[1]}},
@{n="PhoneNumber";e={((([string]($_.TelephoneNumber)).Split(")")[1]).Split(",")[0]).Trim()}},
@{n="Extension";e={([string]($_.TelephoneNumber)).Split(",")[1]}}

我不明白您是如何确定设备类型的!check是在AD查询输出本身中可用的
你可以通过运行-properties *
来检查它,我认为First and Last name也是可用的属性,只是检查一下。

最新更新