0 votes

I've adapted a RegEx to normalize phone numbers, source: http://www.regexplanet.com/cookbook/pho ... index.html.

Import-Module Adaxes
$identity = "%sAMAccountName%"
$mobileNumber = '%mobile%' -ireplace '\D*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4})\D*', '+1 ($1) $3-$5'
Set-AdmUser -Identity $identity -MobilePhone "$mobileNumber"

Testing in the Powershell IDE works perfectly on all phone numbers I gave it. However, when running on numbers formatted at +1 (888) 555-5555, adaxes returned +1+1 (888) 555-5555.

Any guidance on this?

by (1.2k points)
0

I fixed the problem by escaping the input with:

see below.

0

Spoke before I actually tested in adaxes...

The function works once you move the escape to the input as follows:

Import-Module Adaxes  

$identity = "%sAMAccountName%"  

$mobileNumber = \[Regex\]::Escape('%mobile%') -ireplace '\\D\*(\[2-9\]\\d{2})(\\D\*)(\[2-9\]\\d{2})(\\D\*)(\\d{4})\\D\*', '+1 ($1) $3-$5'  
Set-AdmUser -Identity $identity -MobilePhone "$mobileNumber"

If anyone has any suggestions on improving efficiencies, please post.

Thanks!

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello,

The thing is that \D* (non-digits) doesn't include a sequence of characters that contain a plus (+) sign. So, to resolve your issue, you simply need to replace the starting and ending \D* with .* (matches any character). In this case, your zero-level character class will include all characters in the string. The resulting regular expression is as follows:

.*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4}).*

In this case, you don't need to escape anything, just call -ireplace on the value returned by %mobile%:

Import-Module Adaxes

$identity = "%sAMAccountName%"

$mobileNumber = '%mobile%' -ireplace '.*([2-9]\d{2})(\D*)([2-9]\d{2})(\D*)(\d{4}).*', '+1 ($1) $3-$5'
Set-AdmUser -Identity $identity -MobilePhone "$mobileNumber"

Related questions

0 votes
1 answer

Hello I'm trying to run a custom PowerShell script to request a Workspace ONE Access Sync when I change something in our users or groups. Here is the script: $ClientId = "api ... of having to create 6 independent rules with each of them a copy of the script)?

asked Sep 25, 2021 by ygini (240 points)
0 votes
0 answers

We have an inactive user task which runs daily that disables accounts after 30 days of inactivity. We had an example yesterday of a user account which had been disabled ... Are you able to provide any explanation of how this could have happened? Regards Andy

asked Sep 12, 2017 by Andy_W (40 points)
0 votes
1 answer

I am trying to use this script from the script repository: https://www.adaxes.com/script-repository/enable-mfa-with-phone-number-for-a-user-in-microsoft-365-s686.htm I ... if I just didn't install it correctly because Adaxes Powershell doesn't recognize it.

asked Oct 10, 2023 by ocanizales (40 points)
0 votes
0 answers

Whether I try to run a script or manually run the commands to enroll users, users remain unenrolled. Example of a basic script: Import-Module ... ` -QuestionsAndAnswers @{$question1=$answer1;$question2=$answer2} -AdaxesService localhost Adaxes version 2021

asked Mar 27, 2023 by gwadmin (80 points)
0 votes
1 answer

Hi team, I need to update users extensionAttribute6 after adding or removing them from a specific group. This is my setup: Group is updated based on rule set within Adaxes ... would like to update users after they were added or removed from this group. Thanks!

asked Sep 25, 2023 by wintec01 (1.1k points)
3,326 questions
3,026 answers
7,727 comments
544,680 users