regex - 修改 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
所以我需要:
- 添加+1作为国家代码
- 从变量中删除 0,但不删除或修改 Active Directory 值。
- 如果电话号码不是 2digits 4digits 4digits的形式,则显示为无需更改为 +1 Country Code 并删除零。
解决方案
从 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的电话号码匹配。
推荐阅读
- javascript - 带有 D3JS 的 React Native 的 StreamGraph。需要将所有区域合并为一个区域。迭代不起作用
- docker - 如何创建调用 docker 映像的 VSCode 任务,同时设置为在 .md 文件上发生文件更改时运行?
- python - 在 Python 中表示直方图 x 轴内的区间
- c# - CosmosDB 更改更改源处理器名称
- javascript - 在 html5 画布中创建形状时创建了太多对象
- php - ci 休息服务器允许 CORS 不起作用
- angularjs - 无法设置未定义或空引用的属性“属性名称”
- autohotkey - 使用 Enter 按钮提交,而在其他地方正常工作
- swiftui - SwiftUI 选择器没有更新
- python - 如果最适合,如何将每个数据框列的 dtype 更改为浮动?