Примерно так должно работать, просто объединяя значения с диапазоном из массива:
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