我需要一些帮助来修改 PowerShell 脚本以自动格式化 在我现有的广告中将电话和移动内容转换为特定模式。
来源将来自所有存在手机或电话号码的广告用户:
function Get-CountryCode ($CountryCode)
{
switch ($CountryCode)
{
"UK" { "+44" };
"US" { "+1" };
default { "" }
}
}
$paramGetADUser = @{
Filter = "(Enabled -eq 'True') -and (Mail -like '*') -and ( (Phone -like '*') -or (Mobile -like '*') )"
Properties = 'EmailAddress', 'Mobilephone', 'OfficePhone', 'Country'
SearchBase = 'OU=Users,DC=Domain,DC=com'
SearchScope = 'Subtree'
}
$ADUsers = Get-ADUser @paramGetADUser |
Select-Object Name, EmailAddress, UserPrincipalName, SamAccountName, Mobilephone, OfficePhone, Country
ForEach ($ADuser in $ADUsers)
{
If ($ADuser.MobilePhone)
{
Write-Host "Converting '$($ADuser.Name)' $($ADuser.Country) Mobile Phone number: '$($ADuser.MobilePhone)' ..." -ForegroundColor Yellow
$paramSetADUserMobile = @{
Identity = $ADuser.SamAccountName
MobilePhone = { $ADuser.MobilePhone -replace '0(\d)(\d{4})(\d{4})', "$(Get-CountryCode -CountryCode $ADuser.Country) $1 $2 $3" }
ErrorAction = 'SilentlyContinue'
Verbose = $true
}
Set-ADUser @paramSetADUserMobile
Write-Host "`t'$($ADuser.Name)' $($ADuser.Country) Mobile Phone number: '$($paramSetADUserMobile.MobilePhone)' ..." -ForegroundColor Green
}
If ($ADuser.OfficePhone)
{
Write-Host "Converting '$($ADuser.Name)' $($ADuser.Country) Office Phone number: '$($ADuser.OfficePhone)' ..." -ForegroundColor Yellow
$paramSetADUserPhone = @{
Identity = $ADuser.SamAccountName
OfficePhone = { $ADuser.OfficePhone -replace '0(\d)(\d{4})(\d{4})', "$(Get-CountryCode -CountryCode $ADuser.Country) $1 $2 $3" }
ErrorAction = 'SilentlyContinue'
Verbose = $true
}
Set-ADUser @paramSetADUserPhone
Write-Host "`t'$($ADuser.Name)' $($ADuser.Country) Office Phone number: '$($paramSetADUserPhone.OfficePhone)' ..." -ForegroundColor Green
}
}
如何根据国家代码制作以下功能格式手机号码:
来自: 电话:0xxxxxxxxx 手机:0xxxxxxxxx
进入: 电话:+44 x xxxx xxxx 手机:+44 xxx xxx xxx
资料来源:https://www.visitnorthwest.com/essential/uk-telephone-numbers/
根据@Theo 和@MarkAlex 的建议进行了更新,但是,RegEx 模式仍然无法正常工作。
提前谢谢你。
除了在 splatting 哈希表中使用脚本块并且没有转义可扩展字符串中的正则表达式替换组之外,我没有看到您的代码有任何问题。所以本质上:
MobilePhone = { $ADuser.MobilePhone -replace '0(\d)(\d{4})(\d{4})', "$(Get-CountryCode -CountryCode $ADuser.Country) $1 $2 $3" }
应该是(也改变了正则表达式,以匹配预期的格式):
MobilePhone = $ADuser.MobilePhone -replace '0(\d{3})(\d{3})(\d{3})', "$(Get-CountryCode -CountryCode $ADuser.Country) `$1 `$2 `$3"
和:
OfficePhone = { $ADuser.OfficePhone -replace '0(\d)(\d{4})(\d{4})', "$(Get-CountryCode -CountryCode $ADuser.Country) $1 $2 $3" }
应该是:
OfficePhone = $ADuser.OfficePhone -replace '0(\d)(\d{4})(\d{4})', "$(Get-CountryCode -CountryCode $ADuser.Country) `$1 `$2 `$3"
请注意,
`
、$1
和 $2
中的反引号 $3
在可扩展字符串 "..."
中是必需的,否则,PowerShell 会将它们视为变量并尝试将它们插入到字符串中。
我已经研究了很多方法,并尝试使用正则表达式和字符串方法的组合来获得最简单的结果。尝试以下:
$data = 'Telephone: 0xxxxxxxxx Mobile: 0xxxxxxxxx'
$pattern = 'Telephone:\s(?<tele>[^\s]+)\sMobile:\s(?<mobile>.*)'
$data -match $pattern
$newTele = $matches.tele
$newTele = '+44 '+ $newTele.Substring(1,1) + ' ' + $newTele.Substring(2,4) + ' ' + $newTele.Substring(6,4)
$newMobile = $matches.mobile
$newMobile = '+44 '+ $newMobile.Substring(1,1) + ' ' + $newMobile.Substring(2,4) + ' ' + $newMobile.Substring(6,4)
$output = 'Telephone: ' + $newTele + 'Mobile: ' + $newMobile
$output