Page 1 of 1

Unable to call Function to parse the string from AD result

Posted: Mon Mar 18, 2019 8:21 pm
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,

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

Posted: Mon Mar 18, 2019 8:56 pm
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)

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

Posted: Mon Mar 18, 2019 9:03 pm
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 ?

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

Posted: Mon Mar 18, 2019 9:18 pm
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.

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

Posted: Tue Mar 19, 2019 2:56 pm
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)