Powershell 是否将数字输入修改为特定的图案格式?
我如何使用以下代码格式化电话号码以输出结果: +44123456789而不是+44123456789Powershell 是否将数字输入修改为特定的图案格式?,powershell,scripting,active-directory,windows-scripting,Powershell,Scripting,Active Directory,Windows Scripting,我如何使用以下代码格式化电话号码以输出结果: +44123456789而不是+44123456789 函数格式电话号码{ Param( [参数(ValueFromPipeline=$true,位置=0)] [别名('Number')] [字符串]$TelephoneNumber, [参数(位置=1)] [字符串]$DefaultCountryCode='+44' ) 过程{ $formattedNumber=$TelephoneNumber-替换“[\x09]” 如果($formattedNum
函数格式电话号码{
Param(
[参数(ValueFromPipeline=$true,位置=0)]
[别名('Number')]
[字符串]$TelephoneNumber,
[参数(位置=1)]
[字符串]$DefaultCountryCode='+44'
)
过程{
$formattedNumber=$TelephoneNumber-替换“[\x09]”
如果($formattedNumber-match'\A(?\+[1-9]\d | 0)(?\d*)\Z'){
如果($Matches['CountryCode']-eq'0'){
$countryCode=$defaultCountryCode
}
否则{
$countryCode=$Matches['countryCode']
}
$formattedNumber=$countryCode+''+$Matches['Number']
$formattedNumber
}
否则{
写入错误“无法将字符串“$($number)”解析为电话号码!”
}
}
}
“+33 123456789”、“0123456789”、“0 123 456 789”|格式电话号码
类似的方法应该可以工作,只需将值与数组中的一个范围连接起来:
Function Format-TelephoneNumber {
Param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Alias('Number')]
[string]$TelephoneNumber,
[Parameter(Position = 1)]
[string]$DefaultCountryCode = '+44'
)
Process {
$formattedNumber = $TelephoneNumber -replace '[\x09 ]'
If (-not ([string]::ISNULLOREMPTY($TelephoneNumber))) {
If ($formattedNumber -match '\A(?<CountryCode>\+[1-9]\d|0)(?<Number>\d*)\Z') {
If ($Matches['CountryCode'] -eq '0') {
$countryCode = $defaultCountryCode
}
Else {
$countryCode = $Matches['CountryCode']
}
$formattedNumber = $countryCode + ' '
$formattedNumber += -join $Matches['Number'][0..2] + ' '
$formattedNumber += -join $Matches['Number'][3..5] + ' '
$formattedNumber += -join $Matches['Number'][6..8]
$formattedNumber
}
Else {
Write-Error "Unable to parse the string '$($number)' as telephone number!"
}
}
}
}
'+33 123456789', '0123456789', '0 123 456 789' | Format-TelephoneNumber
Owain,谢谢你的回复,它工作得很好。Owain,那么我如何将它与变量一起使用呢$adUser.mobile | Format TelephoneNumber Format TelephoneNumber:无法将字符串“”解析为电话号码!第39行字符:18+$adUser.mobile | Format TelephoneNumber+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[Write Error],WriteErrorException+FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,Format TelephoneNumber Remove[string]请从您的$Telephone number参数声明中删除,然后重试。是否有空值?在解析之前,您可能需要去掉任何空值。不,没有任何空值,因为这是广告中的电话属性。
Function Format-TelephoneNumber {
Param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Alias('Number')]
[string]$TelephoneNumber,
[Parameter(Position = 1)]
[string]$DefaultCountryCode = '+44'
)
Process {
$formattedNumber = $TelephoneNumber -replace '[\x09 ]'
If (-not ([string]::ISNULLOREMPTY($TelephoneNumber))) {
If ($formattedNumber -match '\A(?<CountryCode>\+[1-9]\d|0)(?<Number>\d*)\Z') {
If ($Matches['CountryCode'] -eq '0') {
$countryCode = $defaultCountryCode
}
Else {
$countryCode = $Matches['CountryCode']
}
$formattedNumber = $countryCode + ' '
$formattedNumber += -join $Matches['Number'][0..2] + ' '
$formattedNumber += -join $Matches['Number'][3..5] + ' '
$formattedNumber += -join $Matches['Number'][6..8]
$formattedNumber
}
Else {
Write-Error "Unable to parse the string '$($number)' as telephone number!"
}
}
}
}
'+33 123456789', '0123456789', '0 123 456 789' | Format-TelephoneNumber
+33 123 456 789
+44 123 456 789
+44 123 456 789