Solved

Powershell scripting Active Directory select-object

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
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…

914 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

14 Experts available now in Live!

Get 1:1 Help Now