none
Need to scrape the last 4 digits of a variable into its own variable RRS feed

  • Question

  • Hi everyone, this is my first post. I hope I don't break any rules.

    My dilemma is I am trying to read telephone numbers from AD, remove any characters that is not a number, and input that value into a Skype field. I've been able to accomplish this goal, but I need to go one step further and also add an extension (which is always the last 4 digits of a users telephone) to input into Skype using a specific format: 'tel=1<areacode><number>;ext=<extension>'

    Below is the script I use to import the number from AD(limited to myself for testing) This works great, but I need to scrape the last 4 digits of $phoneNumber and make those digits a new variable such as $ext or something. Currently I am setting everyones ext= value to 0000 via the script.

    Substring will not work since this is not a string, any help is much appreciated!

    #==============================

    $enabledUsers = Get-CsAdUser -Filter {SAMAccountName -eq 'myUserName'}

    foreach ($user in $enabledUsers)

        {

            $phoneNumber = "$user.Phone"

            $phoneNumber = $phoneNumber -replace “[^0-9]”

            $phonenumber = “TEL:+” + $phoneNumber + ";ext=0000"

            Set-CsUser -Identity $user.Identity -LineUri $phoneNumber
            }

    #==================================

    Tuesday, January 24, 2017 5:57 PM

Answers

  • Substring will not work since this is not a string, any help is much appreciated!

    Whatever do you mean?

    The Active Directory attributes that store telephone numbers are, indeed, strings.

    Example:


    $ph = "505-123-4567" -replace '[^-0-9]',''
    # $ph now contains the string "5051234567"
    "TEL:{0};Ext={1}" -f $ph,$ph.Substring($ph.Length-4,4)
    # outputs the string "TEL:5051234567;Ext=4567"
    


    -- Bill Stewart [Bill_Stewart]




    Tuesday, January 24, 2017 6:12 PM
    Moderator

All replies

  • Substring will not work since this is not a string, any help is much appreciated!

    Whatever do you mean?

    The Active Directory attributes that store telephone numbers are, indeed, strings.

    Example:


    $ph = "505-123-4567" -replace '[^-0-9]',''
    # $ph now contains the string "5051234567"
    "TEL:{0};Ext={1}" -f $ph,$ph.Substring($ph.Length-4,4)
    # outputs the string "TEL:5051234567;Ext=4567"
    


    -- Bill Stewart [Bill_Stewart]




    Tuesday, January 24, 2017 6:12 PM
    Moderator
  • Skype does not need to have the dashes removed.  Just build the number:

    $extension='0000'
    $phonenumber = 'TEL;{0}";ext={1}'-f $phoneNumber,$extension


    \_(ツ)_/

    Tuesday, January 24, 2017 6:29 PM
  • Thanks jrv. If the extension in AD were populated this would work too. But our AD does not include extension for 99% of users, hence the need to scrape.
    Tuesday, January 24, 2017 6:47 PM
  • Thanks Bill, this worked. I must have mistyped something, cause I was getting an error that 'System.String does not contain a method named 'Length''.

    this worked, thank you!

    Tuesday, January 24, 2017 6:48 PM
  • For anyone who finds themselves here from google- Here is the end script, this updates the URI field in SfB and adds the ext= field using the last 4 digits of the telephone number field. Thanks to everyone

    $enabledUsers = Get-CsAdUser -Filter {Enabled -ne $Null}

    foreach ($user in $enabledUsers)

        {

        $phoneNumber = $user.phone -replace '[^0-9]',''

        $phoneNumber = "TEL:1{0};Ext={1}" -f $phoneNumber,$phoneNumber.Substring($phoneNumber.Length-4,4)

        Set-CsUser -Identity $user.Identity -LineUri $phoneNumber

    }

    Tuesday, January 24, 2017 7:51 PM