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
89 Views

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

Example: List or table would have:

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
Question by:mrsnetops
• 6
• 5

LVL 68

Expert Comment

ID: 40562534
0

LVL 68

Accepted Solution

Qlemo earned 500 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 }
}

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

0

Author Comment

ID: 40562609
0

Author Comment

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

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 68

Expert Comment

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 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 68 Expert Comment 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 68

Expert Comment

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

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

Author Closing Comment

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

Thanks
0

## Featured Post

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…