修改 PowerShell 以在不更改 AD 属性的情况下以国际格式显示 phone 数字?
Modifying PowerShell to display phone number in International format without changing the AD Attributes?
我需要使用来自 Active Directory 的一些国际 Phone 区号格式修改以下代码,而不修改实际的 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 值。
- 如果phone号码不是2digits 4digits 4digits的形式,则显示为不需要更改为+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.
的电话号码
我需要使用来自 Active Directory 的一些国际 Phone 区号格式修改以下代码,而不修改实际的 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 值。
- 如果phone号码不是2digits 4digits 4digits的形式,则显示为不需要更改为+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.