Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Csv 无法使用Set ADUser更新广告字段_Csv_Powershell_Active Directory - Fatal编程技术网

Csv 无法使用Set ADUser更新广告字段

Csv 无法使用Set ADUser更新广告字段,csv,powershell,active-directory,Csv,Powershell,Active Directory,我正在尝试使用CSV文件和PowerShell脚本更新ActiveDirectory foreach ($user in $users) { Get-ADUser -Identity $user.SamAccountName -Properties * } 返回CSV文件中用户的值,我可以在ActiveDirectory中看到属性名称和值 该代码: $users = Import-Csv -Path C:\ADTest\DirectoryExp

我正在尝试使用CSV文件和PowerShell脚本更新ActiveDirectory

foreach ($user in $users) {            
    Get-ADUser -Identity $user.SamAccountName -Properties *         
}
返回CSV文件中用户的值,我可以在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}
允许我将ActiveDirectory中的
GivenName
值替换为我的CSV中的值。我还有8个值需要更新,当我尝试将这些值添加到代码中时,它不起作用

以下是我尝试过的两种代码尝试:

$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}
}
但两次尝试都会收到相同的错误消息:

这是错误消息:

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 有人能解释一下我做错了什么吗?

这里有部分答案。误解是认为PowerShell和active directory中active directory cmdlet的用户属性之间存在直接关联

查看
Set ADUser
的参数列表。您展示的示例已经介绍过,您不需要使用
-Replace
Set-ADUser
还有一个
-Identity
参数,因此您可以跳过一个cmdlet

Set-ADUser -Identity $user.samaccountname -Surname $user.sn -GivenName $user.givenName
因为您是基于csv工作的,所以您应该小心那些可能不再存在的用户或打字错误

当其中一个内置参数没有所需的属性时,
-Replace
将派上用场。您需要了解,您在其中输入的哈希表使用AD的LDAP属性

如果查看,您将看到Ldap显示名称为
sn
。这就是为什么您会出现错误,因为姓氏在AD中不存在。

这里有部分答案。误解是认为PowerShell和active directory中active directory cmdlet的用户属性之间存在直接关联

查看
Set ADUser
的参数列表。您展示的示例已经介绍过,您不需要使用
-Replace
Set-ADUser
还有一个
-Identity
参数,因此您可以跳过一个cmdlet

Set-ADUser -Identity $user.samaccountname -Surname $user.sn -GivenName $user.givenName
因为您是基于csv工作的,所以您应该小心那些可能不再存在的用户或打字错误

当其中一个内置参数没有所需的属性时,
-Replace
将派上用场。您需要了解,您在其中输入的哈希表使用AD的LDAP属性


如果查看,您将看到Ldap显示名称为
sn
。这就是你出错的原因,因为姓氏在广告中不存在

谢谢你的帮助,非常好。这就是我最后的结局代码。我必须为我在广告中更新的每一项重复这段代码。还不得不留下-姓-萨抛出了一个错误。再次感谢你

IF([string]::IsNullOrEmpty($user.sn))
{        
    Set-ADUser -Identity $user.samaccountname -Surname $null

}else{

    Set-ADUser -Identity $user.samaccountname -Surname $user.sn
}

谢谢你的帮助,非常好。这就是我最后的结局代码。我必须为我在广告中更新的每一项重复这段代码。还不得不留下-姓-萨抛出了一个错误。再次感谢你

IF([string]::IsNullOrEmpty($user.sn))
{        
    Set-ADUser -Identity $user.samaccountname -Surname $null

}else{

    Set-ADUser -Identity $user.samaccountname -Surname $user.sn
}

姓氏在AD中不存在。它是sn。PowerShell cmdlet和AD之间的名称映射不同。PowerShell在某些情况下会提供友好的名称。仅当参数不存在时,才需要使用
-Replace
。例如:
set aduser-name$user.sn-GivenName$user.GivenName
应该可以工作。Technet有更多信息。广告中不存在姓氏。它是sn。PowerShell cmdlet和AD之间的名称映射不同。PowerShell在某些情况下会提供友好的名称。仅当参数不存在时,才需要使用
-Replace
。例如:
set aduser-name$user.sn-GivenName$user.GivenName
应该可以工作。Technet有更多的信息,看起来你不想发布答案,所以我发布了一个社区维基答案。我应该删除它吗?不必重复相同的答案两次。我只删除我的答案。我不想回答,因为这是一个简单的问题。虽然有人会说些什么,但已经过了30分钟,我没有看到任何活动。我只是想把它做完。不,算了吧。无论如何,你是第一个提出解决方案的人。不过,你应该只是发布一个答案而不是评论。看起来你不想发布答案,所以我发布了一个社区维基答案。我应该删除它吗?不必重复相同的答案两次。我只删除我的答案。我不想回答,因为这是一个简单的问题。虽然有人会说些什么,但已经过了30分钟,我没有看到任何活动。我只是想把它做完。不,算了吧。无论如何,你是第一个提出解决方案的人。不过,我应该只是发布一个答案而不是评论。