【问题标题】:Modifying PowerShell to display phone number in International format without changing the AD Attributes?修改 PowerShell 以在不更改 AD 属性的情况下以国际格式显示电话号码?
【发布时间】:2019-04-30 01:00:32
【问题描述】:

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

$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

所以我需要:

  • 添加 +1 作为国家代码
  • 从变量中删除 0,但不删除或修改 Active Directory 值。
  • 如果电话号码不是2digits 4digits 4digits的形式,则显示为无需更改为+1 Country Code并删除零。

【问题讨论】:

    标签: regex powershell scripting active-directory windows-scripting


    【解决方案1】:

    从 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 的电话号码匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2010-10-27
      相关资源
      最近更新 更多