Link to home
Start Free TrialLog in
Avatar of Joseph Daly
Joseph DalyFlag for United States of America

asked on

Powershell scripting Active Directory select-object

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

Avatar of KenMcF
KenMcF
Flag of United States of America image

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

Avatar of Joseph Daly

ASKER

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.
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}
}}
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.
ASKER CERTIFIED SOLUTION
Avatar of KenMcF
KenMcF
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial