Unable to call Function to parse the string from AD result

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Unable to call Function to parse the string from AD result

Post by ITEngineer »

I'm unable to call Function to parse the string from AD result using 10x different combination like below?

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
Can anyone here please assist me in correcting the code above?

Thanks,
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to call Function to parse the string from AD result

Post by jvierra »

How to format a telephone number:


$n = '2123346789'
'({0}){1}-{2}' -f $n.SubString(0,3),$n.Substring(3,3),$n.SubString(6,4)
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Unable to call Function to parse the string from AD result

Post by ITEngineer »

jvierra wrote: Mon Mar 18, 2019 8:56 pm How to format a telephone number:


$n = '2123346789'
'({0}){1}-{2}' -f $n.SubString(0,3),$n.Substring(3,3),$n.SubString(6,4)
That's good to know,

so how can I store that formatted number into a variable called $Formatted ?
/* IT Engineer */
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Unable to call Function to parse the string from AD result

Post by ITEngineer »

The result is still an error?

Code: Select all

#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

$Formatted = '({0}){1}-{2}' -f $adUser.mobile.ToString().SubString(0,3),$n.Substring(3,3),$n.SubString(6,4)
Write-Host "This is processed using Function: +44" + "$($Formatted)" -ForegroundColor Green
I wanted to be able to get the result as: +44 123 345 345 for example.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to call Function to parse the string from AD result

Post by jvierra »


$n = '44123345345'
'+{0} {1} {2} {3}' -f $n.SubString(0,2),$n.Substring(2,3),$n.SubString(5,3),$n.SubString(8,3)
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked