首页 > 解决方案 > 修改 PowerShell 以在不更改 AD 属性的情况下以国际格式显示电话号码?

问题描述

我需要使用 Active Directory 中的一些国际电话区号格式修改以下代码,而不修改实际的 AD 属性值:

$defaultTelephone = '1800 552 001'

#Get Active Directory information for the currently logged on user
$sysInfo = New-Object -ComObject 'ADSystemInfo'
$userDN = $sysInfo.GetType().InvokeMember('UserName', 'GetProperty', $null, $sysInfo, $null)
$adUser = [ADSI]"LDAP://$($userDN)"
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($sysInfo)

#Get the phone number from the Active Directory and assign it into the International phone country code format
$IntlPhoneNumber = $(If ($ADUser.telephoneNumber) { $ADUser.telephoneNumber.ToString() }
    Else { $defaultTelephone })

$IntlPhoneNumber

在上面的脚本中,它从现在设置为 08 8211 8911 的 AD 属性中提取信息

我想显示为$IntlPhoneNumber的值是 + 1 8 8211 8911

所以我需要:

标签: regexpowershellscriptingactive-directorywindows-scripting

解决方案


从 Active Directory 读取号码后,检查是否应更改并在必要时进行。像这样,数字不会在 Active Directory 中改变(反正没有写操作):

$IntlPhoneNumber = "08 8211 8911"

if($IntlPhoneNumber -match '^\d{2}(\s\d{4}){2}$'){
    $IntlPhoneNumber = $IntlPhoneNumber -replace '^0', '+1 '
}

$IntlPhoneNumber # +1 8 8211 8911

RegEx ^\d{2}(\s\d{4}){2}$仅与格式为 2digits 4digits 4digits的电话号码匹配。


推荐阅读