I've got an problem creating new users in Active Directory. I've got a .csv file where I specified as one of many headers "location" with the City where the User will be situated, now I want to create the user in an OU that matches the "location" entry.
For example: "Location" is Dresden and OU is "OU=Users,OU=Dresden,OU=Germany,DC=acme,DC=com"
The same when location is "Munich" "OU=Users,OU=Munich,OU=Germany,DC=acme,DC=com"
I played around with Wildcars etc. but it seems there is a missing link in my brain, how can I create a variable- coming from the matching correct OU to set the correct -path command?
$Domain = 'dc=acme,dc=com'
$OUs = @( #what to put in here to make this to a matching variable?
"OU=Users,OU=Munich,OU=Germany,",
"OU=Users,OU=Munich,OU=Germany,",
)
Not sure If you can get my problem now :)
Dschordge
If you pattern is always the same, you can just create a "template string" and drop the location
value from the csv into the string with the string format operator (-f
):
$Domain = 'dc=acme,dc=com'
$OUtmpl = "OU=Users,OU={0},OU=Germany,$Domain"
foreach($newUser in Import-Csv path\to.csv)
{
$OU = $OUtmpl -f $newUser.location
# create user in $OU here
}