Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1297
  • Last Modified:

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

0
Joseph Daly
Asked:
Joseph Daly
  • 3
  • 2
1 Solution
 
KenMcFCommented:
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
 
Joseph DalyAuthor Commented:
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
 
KenMcFCommented:
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
 
Joseph DalyAuthor Commented:
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
 
KenMcFCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now