假设我在OU树下面:
DataManagement └─Country ├─Germany │ └─Users │ ├─Laptops │ └─Computers ├─France │ └─Users │ ├─Laptops │ └─Computers etc.
我想更新OU中的特定容器,例如法国的笔记本电脑组中的用户。如果我想从CSV导入用户该怎么做?下面的代码检查并更新所有OU。不幸的是,我不知道如何选择一个特定的容器。有什么建议吗?
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
}
}
答案 0 :(得分:0)
如果只有SamAccountName
来标识用户,则可以使用Get-ADUser
检索用户帐户对象,并在调用DistinguishedName
之前检查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
}
}
答案 1 :(得分:0)
嗯,如果您然后向我们展示了csv的内容(部分),将会有所帮助。
但是,我认为这对您有用:
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}
}
}