Unable to call Function to parse the string from AD result

Ask your Windows 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.
Locked
User avatar
ITEngineer
Posts: 167
Joined: Wed Oct 12, 2011 10:52 am

Unable to call Function to parse the string from AD result

Post by ITEngineer » 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?

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: 13935
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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)

User avatar
ITEngineer
Posts: 167
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Mon Mar 18, 2019 9:03 pm

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: 167
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Mon Mar 18, 2019 9:18 pm

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: 13935
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Mar 19, 2019 2:56 pm


$n = '44123345345'
'+{0} {1} {2} {3}' -f $n.SubString(0,2),$n.Substring(2,3),$n.SubString(5,3),$n.SubString(8,3)

Locked