Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing/Removing SPECIFIC Characters From AD Fields 1

Status
Not open for further replies.

ncotton

IS-IT--Management
Jan 27, 2006
2,841
GB
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
 
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
[b]#	 $user.phone = $mod_phone
#	 $user.othertelephone = $mod_othertelephone[/b]

	 ## 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.
 
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.
 
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:
DisplayName              : Phone User
Fax                      : +44 (0)1253 987654
FirstName                : Phone
HomePhone                : +44 (0)1253 121212
LastName                 : User
Phone                    : +44 (0)1253 123456

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

Code:
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
 
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 [COLOR=#EF2929]"\(0\) "[/color], ""
	 $mod_fax = $fax -replace "\(0\) ", ""
	 $mod_homephone = $homephone -replace "\(0\) ", ""
	 ## Write phone numbers back to user
#	 [COLOR=#EF2929]set-user $user -Phone $mod_phone -Fax $mod_fax -HomePhone $mod_homephone[/color]
	 ## 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.
 
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.
 
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
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top