INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Replacing/Removing SPECIFIC Characters From AD Fields

Replacing/Removing SPECIFIC Characters From AD Fields

(OP)
Good afternoon guys and girls.
I would love it if you could help with me with my quandry.
Currently, I have an active directory structure with users all having 4 separate phone numbers.

All phone numbers are stored in the following format.
+44 (0) 1234 987654

HOWEVER, I need to get all the numbers for all users to read just
+44 1234 987654 to meet E.164 standards.

The reason is we are moving over to Android based mobile devices and Android seems to be the only mobile OS that DOESN'T strip out 0's between brackets... it strips out the brackets (because they aren't valid numeric values, but it still leaves the 0 (trunk prefix). I have tried prefixer and other pre-diallers but cant see a simple way of doing it (without having to resort to separate rules for each international prefix on each individual phone). If anyone know's a better way then, I'm open to offers.

Thanks in advance.

Neil

RE: Replacing/Removing SPECIFIC Characters From AD Fields

Neil,
Could you do something like this? (I don't have anywhere to test this, so it could contain errors)

CODE

$users = Get-User -OrganizationalUnit "<FQDN to OU containing the users>"
foreach ($user in $users)
	{
	 ## Get phone number
	 $phone = $user.phone
	 $othertelephone = $user.othertelephone
	 ## Change phone numbers
	 $mod_phone = $phone -replace "(0)", ""
	 $mod_othertelephone = $othertelephone -replace "(0)", ""
	 ## Write phone numbers back to user
#	 $user.phone = $mod_phone
#	 $user.othertelephone = $mod_othertelephone

	 ## Debug
	 Write-host $user	$mod_phone	$mod_othertelephone
	} 

I just grabbed a couple of examples of possible phone numbers. You'd have to see which are in use and then modifiy the script accordingly. I always like to do a test run before making the changes. Uncomment the bolded lines once you have this working. You can grab a user and then search it to find out the phone numbers in use.

CODE

$me = Get-User <userid>
$me | fl 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Replacing/Removing SPECIFIC Characters From AD Fields

Didn't this of this until now. You might need to remove the extra space after the "(0)" is removed; you will have two spaces, together. Change those lines in the code by adding a space after the closing parenthsis, like this:

CODE

$mod_phone = $phone -replace "(0) ", "" 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Replacing/Removing SPECIFIC Characters From AD Fields

(OP)
Hi Blister.
We seem to be pretty much there. However the debug output shows that it strips the 0 out of the bracket, however it leaves the bracket. Also, when I take out the comments to write back, it doesn't seem to overwrite the user :-/

CODE --> Before

DisplayName              : Phone User
Fax                      : +44 (0)1253 987654
FirstName                : Phone
HomePhone                : +44 (0)1253 121212
LastName                 : User
Phone                    : +44 (0)1253 123456 

CODE --> debug

uk.agfp/PhoneTest/Phone User +44 ()1253 123456 +44 ()1253 121212 +44 ()1253 987654 

CODE --> Write

DisplayName              : Phone User
Fax                      : +44 (0)1253 987654
FirstName                : Phone
HomePhone                : +44 (0)1253 121212
LastName                 : User
Phone                    : +44 (0)1253 123456 

So it detects the relevant fields, and removes the zero (not sure why it leaves the parentheses, but doesn't write it back to the user. Strange.

Neil J Cotton

RE: Replacing/Removing SPECIFIC Characters From AD Fields

This should fix it:

CODE

$users = Get-User -OrganizationalUnit "<FQDN to OU containing the users>"
foreach ($user in $users)
	{
	 ## Get phone numbers
	 $phone = $user.phone
	 $fax = $user.fax
	 $homephone = $user.homephone
	 ## Change phone numbers
	 $mod_phone = $phone -replace "\(0\) ", ""
	 $mod_fax = $fax -replace "\(0\) ", ""
	 $mod_homephone = $homephone -replace "\(0\) ", ""
	 ## Write phone numbers back to user
#	 set-user $user -Phone $mod_phone -Fax $mod_fax -HomePhone $mod_homephone
	 ## Debug
	 Write-host $user	$mod_phone	$mod_othertelephone
	} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Replacing/Removing SPECIFIC Characters From AD Fields

Sorry I've been running on very little sleep lately. I forgot to change the debug line. It should be:

Write-host $user $mod_phone $mod_fax $mod_homephone


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Replacing/Removing SPECIFIC Characters From AD Fields

(OP)
That's worked a treat, thanks blister.

I made some amendments and managed to get myself in a little bit of a mess. I added a mobile phone number field, and wasn't paying attention and I passed the LAND LINE (phone) AD field to it. So now I've written over everybodies mobile phone field with a duplicate landline field. I've manually updated all the users that SHOULD have mobile fields filled in with the correct mobile number manually. But we now have a few hundred people that don't have mobiles with an incorrect mobile field.

Is there any easy way that I can go through looking for instances where mobilephone = phone, and replace with ""?

Cheers.
Neil

Neil J Cotton

RE: Replacing/Removing SPECIFIC Characters From AD Fields

CODE

$users = Get-User -OrganizationalUnit "<FQDN to OU containing the users>"
foreach ($user in $users)
	{
	 ## Get phone numbers
	 $phone = $user.phone
	 $mobile = $user.mobilephone
	 ## Compare phone number to mobile number
	 if ($phone -eq $mobile)
		{
#		 set-user $user -MobilePhone ""
		 ## Debug
		 write-host $user	$phone	$mobile
		}
	} 

Hope that helps.


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Replacing/Removing SPECIFIC Characters From AD Fields

(OP)
Blister, you are an absolute star. That worked perfectly, and also given me a great footstep on to doing other things myself, never really programed scripts in powershell before so it's pretty new to me. Only ever run single exchange commands really. So a big BIG thank you for your effort. Very precise and worked spot on.

Thank you,

Neil J Cotton

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close