Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create up update AD user account

Status
Not open for further replies.

Lucidity

IS-IT--Management
Aug 21, 2002
12
US
I've been working on creating a new script to further automate our user on-boarding process and I've run into something of a wall. I want to be able to populate different aspects of a new user account with address details, move to a specific OU, etc. based on that users physical location. So the way this works is I'll either get a csv file from HR with the users location information but i also have code i can uncomment to prompt for the same details for someone running the script without the csv file.

For address details, I've got a csv file: office,streetaddress,city,state,zip. So based on the office a user will work from, I want to populate variables based on that location. So I am trying to do something like this:

$addressFile = "\\path-to-csv\addresses.csv"
$office = "Okemos"
$username='user@domain.com'
Import-Csv $addressFile|? Office -eq $office | Where-Object {

{
$streetaddress=$addressFile.streetaddress
$city=$addressfile.city
$state=$addressFile.stateorprovence
$zipcode=$addressFile.postalcode
$country=$addressFile.country
set-aduser -identity $username -replace @{"streetAddress"=$streetaddress}
set-aduser -identity $username -replace @{"postalcode"=$zipcode}
set-aduser -identity $username -replace @{"Country"=$Country}
set-aduser -identity $username -city $addressfile.city
set-aduser -identity $username -replace @{"stateorprovence"=$state}

}
}

And what's happening is I'm not getting those variables populated with any data.

I want to do the same thing with the OU the account is placed in as well. Same principal, just different data. The OU is based on department and location. So I created a csv with all possible combinations of office and department and their associated OU. For example, if a user will be in St. Louis Sales, the OU the account goes in is for that specific office/department. The reason for this is mostly application of policy.

It is entirely possible that I am approaching this wrong and shouldn't be using a csv but rather excel docs or possibly even a sql db (though I feel that's overkill for something like this), however, I'd appreciate any guidance on this please. Once i have the theory down for what I'm wanting to accomplish i can take it from there.

Thanks all.
 
I would import the entire address file. If you're running the import from a file supplied by HR, you'll need to reference it multiple times, so it would be more efficient to get it once and repeatedly search the import for the data you need. If you're running it without the file from HR you can then push the array to a list and have the user pick the location from that.

Are all your users in the same OU or do you have one OU per location.

We have multiple OUs. So I have a list of locations from which the admin running the user creation script can pick. Each location has an associated OU. Each OU has the address, city, state (if applicable), country (c, co, countrycode) and other attributes set. So when the admin picks the location, I pull all of attributes directly from the OU and apply them to the new user.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
By importing the entire file, do you mean use import-csv? Or do think it better to go a step further and create an array with that data?

My users are scattered about in multiple OU's. Like you, our users are in specific OU's based on their location and then sub containers based on department.
 
Yes. Import-Csv creates an array. The first row of the csv file is the attribute names.

Code:
Example of file
row 1: FirstName,MiddleInitial,LastName,Office,Street,City,State,Zip
row 2: John,Q,Public,San Francisco,1234 Main Street,San Francisco,CA,94107

Code:
$users = Import-Csv <path2file>
forach ($user in $users)
  {
   $fname = $user.FirstName
   $lname = $user.LastName
   .
   .
   .
   <Create User>
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top