无法使用 Set-ADUser 更新 AD 字段 [英] Unable to update AD fields with Set-ADUser
问题描述
我正在尝试使用 CSV 文件和 PowerShell 脚本更新 ActiveDirectory 的信息.
I am attempting to update ActiveDirectory with information using a CSV file and a PowerShell script.
foreach ($user in $users) {
Get-ADUser -Identity $user.SamAccountName -Properties *
}
在我的 CSV 文件中返回用户的值,我可以在 ActiveDirectory 中看到属性名称和值.
returns the values for the users in my CSV file and I can see the property names and values in ActiveDirectory.
还有这段代码:
$users = Import-Csv -Path C:\ADTest\DirectoryExport2.csv
foreach ($user in $users) {
#Search and Update existing attributes
Get-ADUser -Identity $user.samaccountname |
Set-ADUser -Replace @{GivenName=$user.givenName}
允许我用我的 CSV 中的值替换 ActiveDirectory 中的 GivenName
值.我还有 8 个需要更新的值,当我尝试将这些值添加到代码中时,它不起作用.
allows me to replace the GivenName
value in ActiveDirectory with the value in my CSV. I have 8 more values that I need to update and when I try to add these to the code it does not work.
这是我尝试过的 2 次代码尝试:
These are the 2 code attempts I have tried:
$users = Import-Csv -Path C:\ADTest\DirectoryExport2.csv
foreach ($user in $users) {
#Search and Update existing attributes
Get-ADUser -Identity $user.samaccountname |
Set-ADUser -Replace @{GivenName=$user.givenName}
Get-ADUser -Identity $user.samaccountname |
Set-ADUser -Replace @{Surname=$user.sn}
}
我也试过这个:
$users = Import-Csv -Path C:\ADTest\DirectoryExport2.csv
foreach ($user in $users) {
#Search and Update existing attributes
Get-ADUser -Identity $user.samaccountname |
Set-ADUser -Replace @{GivenName=$user.givenName;Surname=$user.sn}
}
但我在两次尝试中都收到相同的错误消息:
But I get this same error message for both attempts:
这是错误信息:
Set-ADUser : The specified directory service attribute or value does not exist
Parameter name: Surname
At line:5 char:55
+ Get-ADUser -Identity $user.samaccountname | Set-ADUser <<<< -replace @{Surname=$user.sn}
+ CategoryInfo : InvalidArgument: (CN=Test1User1,O...-sheriff,DC=net:ADUser) [Set-ADUser], ArgumentException
+ FullyQualifiedErrorId : The specified directory service attribute or value does not exist
Parameter name: Surname,Microsoft.ActiveDirectory.Management.Commands.SetADUser
参数名称 Surname
确实存在于 ActiveDirectory 中,因为当我运行上面的第一个代码时我可以看到它是该查询的结果.如您所见,它们是空白的.
The parameter name Surname
does exist in ActiveDirectory because I can see it when I run the first code above below is the results of that query. As you can see they are blank.
State :
StreetAddress :
Surname :
Title :
谁能解释一下我在这里做错了什么?
Can anyone explain what I am doing wrong here?
推荐答案
感谢您的帮助,非常好.这就是我最终的结局代码.我不得不为我在 AD 中更新的每个项目重复此代码,使其有效.还不得不留在 -Surname 中 -sa 抛出错误.再次感谢您
Thanks for your help it was very good. This is what I ended up with for my finale code. I had to repeat this code for each item I update in AD put it works. Also had to stay with the -Surname the -sa threw an error. Again thank you
IF([string]::IsNullOrEmpty($user.sn))
{
Set-ADUser -Identity $user.samaccountname -Surname $null
}else{
Set-ADUser -Identity $user.samaccountname -Surname $user.sn
}
这篇关于无法使用 Set-ADUser 更新 AD 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!