Unable to call Function to parse the string from AD result
Posted: Mon Mar 18, 2019 8:21 pm
I'm unable to call Function to parse the string from AD result using 10x different combination like below?
Can anyone here please assist me in correcting the code above?
Thanks,
Code: Select all
Function Format-TelephoneNumber
{
Param (
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Alias('Number')]
[string]$TelephoneNumber,
[Parameter(Position = 1)]
[string]$DefaultCountryCode = '+41'
)
Process
{
$formattedNumber = $TelephoneNumber -replace '[\x09 ]'
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!"
}
}
}
#Get Active Directory information for current 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)
Write-Host "This is raw from AD: $($adUser.mobile.ToString())" -ForegroundColor Yellow
$StringPhone = $adUser.mobile.ToString()
Format-TelephoneNumber -Number "$StringPhone"
Format-TelephoneNumber -Number '$StringPhone'
Format-TelephoneNumber -Number $StringPhone
Format-TelephoneNumber("$StringPhone")
Format-TelephoneNumber('$StringPhone')
Format-TelephoneNumber $StringPhone
Format-TelephoneNumber "$StringPhone"
$StringPhone | Format-TelephoneNumber
"$StringPhone" | Format-TelephoneNumber
'$StringPhone' | Format-TelephoneNumber
$Formatted = Format-TelephoneNumber("$StringPhone")
Write-Host "This is processed using Function: $Formatted" -ForegroundColor Green
Thanks,