Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell scripting Active Directory select-object

Posted on 2010-11-15
5
Medium Priority
?
1,285 Views
Last Modified: 2012-05-10
I am trying to write a powershell script that will grab the users telephone number from active directory, select a portion of it (last 4 numbers), concatenate some numbers onto that, and then add that back into Active directory as the IP phone attribute.

I was able to do this successfully by exporting data to a csv modifying and then re-importing but I know there is a way of doing this all from the command line.

The command I used to dump the telephone number was
get-qaduser -phonenumber xxx-xxx-xxxx | select-object phonenumber

I am not sure how to actually get and manipulate the value that is returned from that command. I tried
$phone = get-qaduser -phonenumber xxx-xxx-xxxx | select-object phonenumber

Which seems to work but it stores the whole output not just the telelphone number.

So I guess my first two questions are
1. How can i capture only the 10 digit telephone number
2. How can I manipulate that in powershell

0
Comment
Question by:Joseph Daly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 34136871
Do all users use the same format for the phone number?

for example

(123)-456-7890

if so you can try this

$phone = (get-qaduser USERNAME).phonenumber
$phone.substring(10,4)

Just change the first number to fit your needs.

so if you numbers are

(123)456-7890

it would be

$phone = (get-qaduser USERNAME).phonenumber
$phone.substring(9,4)

then to change

note:this has not been tested so test in dev first.

$newphone = $phone - replace "$($phone.substring(9,4))", "1234"
set-qaduser USERNAME -telephonenumber $newphone

0
 
LVL 35

Author Comment

by:Joseph Daly
ID: 34136961
Thats pretty cool. I never knew about the . method to return data. Ive always seen select-object used.

I dont think that will work for what im trying to do though. Im not trying to get the phone number for an individual person but all phone numbers that match a specific pattern.

Basically what I need to do it take the number in the telephone field, take the last 4 digits, add 400 in front of them and then put that in the IP phone filed in AD. I wanna do this for everyone that has a number that matches a certain pattern.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34137090
I think I understand what you are trying to do. Can you see if this will work for you, if not can you give me an example.

$users = get-qaduser
foreach ($user in $users){
if ($user.phonenumber -match "1234"){
$phone = user.phonenumber
$ipphone = "(123)-400-$($phone.substring(9,4))"
set-qaduser -objectattribute@{ipphone=$ipphone}
}}
0
 
LVL 35

Author Comment

by:Joseph Daly
ID: 34137189
I didnt run that but looking at the code i dont think its gonna do what im looking for. So here is an example of what id like to do.

Given several users
User1: 617-555-abcd
User2: 617-555-hijk
User3 617-123-xxxx

So get-qaduser -phonenumber 617-555* would only affect user1 and user2. I want to grab the last 4 digits of the users numbers who match the filter of 617-555* and then put 400 in front of them then store that in IP phone.

The output would be
User1: 400abcd
User2: 400hijk
user3: unchanged

I hope this makes sense.
0
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 34137258
ok, that makes sense I think.

$users = get-qaduser | where {$_.telephonenumber -match "617-555"}
foreach ($user in $users){
$phone = $user.phonenumber
$ipphone = "400$($phone.substring(9,4))"
set-qaduser $user.dn -objectattribute@{ipphone=$ipphone}
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question