Solved

Powershell scripting Active Directory select-object

Posted on 2010-11-15
5
1,241 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
  • 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now