1

Let's say I have below OU tree:

DataManagement
└─Country
  ├─Germany
  │ └─Users
  │   ├─Laptops
  │   └─Computers
  ├─France
  │ └─Users
  │   ├─Laptops
  │   └─Computers
  etc.

I would like to update specific container in OU, for example, users in laptops group in France. How to do that if I would like to import users from CSV? Below code check and update all OU. Unfortunately, I have no idea how to select a specific container. Any suggestions?

Import-Module ActiveDirectory
$Userscsv = Import-Csv D:\areile\Desktop\adtest.csv

foreach ($User in $Userscsv) {
    Set-ADUser $User.SamAccountName -Replace @{
        Division = $User.Division;
        Office   = $User.Office;
        City     = $User.City
    }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Wiktor
  • 581
  • 1
  • 12
  • 23

2 Answers2

1

Ah, it would have helped if you have shown us (part) of the content of the csv then.

However, i think this will work for you:

Import-Module ActiveDirectory
$UsersCsv = Import-Csv D:\areile\Desktop\adtest.csv

$SearchBase = "<DISTINGHUISHEDNAME-OF-THE-FRENCH-USERS-OU>"

foreach ($usr in $UsersCsv) {
    $adUser = Get-ADUser -Filter {EmailAdress -eq '$($usr.Email)'} -SearchBase $SearchBase -Properties Division,Office,City,EmailAddress
    if ($null -ne $adUser) {
        Set-ADUser $adUser.SamAccountName -Replace @{Division = $usr.Division; Office = $usr.Office; City = $usr.City}
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • This is my csv, file - Name,Surname,Phone,Email, name1,surnam1,+42222222222,mail@test.loc – Wiktor Jul 15 '18 at 13:46
  • And this is an arror while i tried to compile this -> Get-ADUser : Error parsing query: 'Name -eq name1' Error Message: 'syntax error' at position: '10'. At C:\test\PhoneReplace1.ps1:9 char:15 + $adUser = Get-ADUser -Filter "Name -eq $($usr.Name)"-SearchBase $SearchBase ... + CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingExc – Wiktor Jul 15 '18 at 13:47
  • That's because the field in your CSV appears to be named `name1`, not `name` as you stated earlier. Anyway, i see you also have a field `Email` in there and that might be even better to use: `Get-ADUser -Filter "EmailAdress-eq '$($usr.Email)'" -SearchBase $SearchBase -Properties Division,Office,City` – Theo Jul 15 '18 at 14:00
  • I have just edited my answer to use the EmailAddress wich is a value in your csv called `mail` – Theo Jul 15 '18 at 19:22
  • I just edit it to my use and - Import-Module ActiveDirectory $users = Import-Csv 'C:\test\testfile.csv' $users $SearchBase = "OU=Deployment,OU=Poland,OU=Users,OU=Laptop,DC=test,DC=loc" $SearchBase #add mobile phone to unique users foreach($usr in $users){ $adUser = Get-ADUser -Filter "EmailAdress -eq '$($usr.Email)'" -SearchBase $SearchBase -Properties FirstName, LastName, MobilePhone,EmailAddress if ($null -ne $adUser) { Set-ADUser $adUser.SamAccountName -Replace @{MobilePhone = $usr.MobilePhone} } } – Wiktor Jul 15 '18 at 19:33
  • this is csv - EmailAddress,FirstName,LastName,MobilePhone wkostrzewski@test.loc,Wiktor,Kostrzewski,123-555-1211 – Wiktor Jul 15 '18 at 19:33
  • An error look like this -> Get-ADUser : One or more properties are invalid. Parameter name: FirstName At C:\test\PhoneReplace1.ps1:9 char:15 + $adUser = Get-ADUser -Filter "EmailAdress -eq '$($usr.Email)'" -SearchBase $ ... – Wiktor Jul 15 '18 at 19:34
  • the quotes were a typo. use `{EmailAdress -eq '$($usr.Email)'}`. BUT... How come now the field in the csv is suddenly called `EmailAddress` when first it was called `Email` ?? PLease give the correct field headers in the csv otherwise it will never work. – Theo Jul 15 '18 at 19:49
  • Ok i changed the csv to basic data now -> Email,LastName wkostrzewski@test.loc, Kostrzewski but error is like this -> Email ADUser : Directory object not found At C:\test\PhoneReplace1.ps1:9 char:15 + $adUser = Get-ADUser -Filter {EmailAdress -eq '$($usr.Email)'} -SearchBase $ ... – Wiktor Jul 15 '18 at 19:58
  • and code is like this -> foreach ($usr in $users) { $adUser = Get-ADUser -Filter {EmailAdress -eq '$($usr.Email)'} -SearchBase $SearchBase -Properties EmailAddress,Surname if ($null -ne $adUser) { Set-ADUser $adUser.SamAccountName -Replace @{EmailAddress = $usr.Email; Surname = $usr.LastName;} } } – Wiktor Jul 15 '18 at 19:59
  • I am using powershell v 4 – Wiktor Jul 15 '18 at 20:00
  • so what do you get when you simply type `Get-ADUser -Filter {EmailAdress -eq 'wkostrzewski@test.loc'}` – Theo Jul 15 '18 at 20:17
  • I found that removing -SearchBase $SearchBase -Properties Division,Office,City,EmailAddress this piece of code allows program to run (without updating mobile entry) – Wiktor Jul 15 '18 at 20:55
  • Check your searchbase. The user you are trying to update is nog in the OU specified – Theo Jul 16 '18 at 15:28
0

If you only have the SamAccountName to identify the user, you can retrieve the user account object using Get-ADUser and inspect the DistinguishedName attribute before calling Set-ADUser:

$BaseOU = 'OU=Laptops,OU=Users,OU=France,DC=domain,DC=tld'

ForEach ($User in $Userscsv)
{
    Get-ADUser $User.sAMAccountName |Where-Object {$_.DistinguishedName -like "*,$BaseOU"} |Set-ADUser -Replace @{
        Division = $User.Division; 
        Office = $User.Office; 
        City = $User.City
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Unfortunately, CSV have only name, surname and phone number and i would like to update phone number – Wiktor Jul 15 '18 at 10:22