?
Solved

Can someone could help me write a Power shell Script to Change attribute within AD for every user by comparing to a table or file

Posted on 2015-01-21
11
Medium Priority
?
95 Views
Last Modified: 2015-01-28
We need a power shell script the will compare the Address field in AD with an address in a list, table, or file.  Only If it matches any address in list or file it will place the corresponding Site Number - City into the Office Field in AD. If the script cannot find a matching address or the AD address field is blank it should skip to the next user in AD.

So the office will have "543 - Howard", "234 - Walker" or "972 - Clearwater" (Except without Quotes).

Example: List or table would have:

Company Address         City                      SiteNumber
12345 Bolen RD             Plinkdon                    258
67890 Tree Circle          Howard                      543
55555 Parrot Dr.           Clearwater                 972
23423 Box Blvd             Walker                         234
98093 Hwy 6                 Bennington                 451


Thanks very much for the help in Advance. I hope it  was detailed enough.

Jeff McCormick
0
Comment
Question by:mrsnetops
[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
  • 6
  • 5
11 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40562534
There is no "Address" field in AD. You've got City, PostalCode, Company, and many more. Or are you talking about StreetAddress?
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40562577
If the latter, try this. It is in test mode, only showing what it would do. Remove the -whatif to allow changes to be written.
$sites = @{}
import-csv c:\temp\ee\sites.csv | % {  $sites.Add($_."Company Address", $_.SiteNumber +" - " + $_.City) }

Get-ADUser -Filter {StreetAddress -like '*'} -property StreetAddress, PostalCode, City, Office | % {
  if ($addr = $sites[$_.StreetAddress])  { Set-ADUser $_ -Office $addr -whatif }
}

Open in new window

The input file needs to be in CSV format, like:
Company Address,City,SiteNumber
12345 Bolen RD,Plinkdon,258
67890 Tree Circle,Howard,543
55555 Parrot Dr.,Clearwater,972
23423 Box Blvd,Walker,234
98093 Hwy 6,Bennington,451

Open in new window

0
 

Author Comment

by:mrsnetops
ID: 40562609
Yes I am talking about Street in the address tab.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:mrsnetops
ID: 40562686
Qlemo, Thanks for the script.  Could you walk me through you script and tell me what your doing. I think I have it but their some parts  I am not understanding.

Also this skip anything it doesn't match  correct?
0
 

Author Comment

by:mrsnetops
ID: 40562723
Is this going to only put  " Site - City" in office field?  Which is way I need it. Don't need  street address, state or postal. Street address is only used to compare. Like I said I am probably misunderstanding what you doing.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40563133
Yes, the script only changes something if it finds a match, and then sets "SiteNumber - City". The Get-ADUser and Set-ADUser shouldn't be an issue to understand.
$sites is more interesting. This is a hash table, allowing to use the street address as index to get another string (the Office value).
Please feel free to ask for more help, but be specific. It is very helpful for understanding to try parts by yourself - a script language is best suited for that.
0
 

Author Comment

by:mrsnetops
ID: 40563145
What does this do?  {StreetAddress -like '*'}

How does the $addr play role here. I don't see it referenced anywhere else in the script?

 if ($addr = $sites[$_.StreetAddress])  { Set-ADUser $_ -Office $addr -whatif }
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40563163
The if does several things. It sets the $addr to the intended Office string, and checks if it is empty. If not, execute the Set-Set-ADUser command. $addr is empty if a match is found in the list.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40563170
The filter expression enforces to get only AD users which have a StreetAddress set up. We do not need to get users without street address...
0
 

Author Comment

by:mrsnetops
ID: 40563193
Okay thank you. I will check it out tomorrow on a test AD.
0
 

Author Closing Comment

by:mrsnetops
ID: 40575730
Responded really quick even followed up to answer some questions about the script.

Thanks
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
Let's recap what we learned from yesterday's Skyport Systems webinar.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

770 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