powershell - 用Powershell add-adgroupmember结束

标签 powershell active-directory

数小时以来,我一直在尝试使用Add-ADGroupmember将用户从CSV添加到组中。我总是得到错误-

Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again."



这是我正在使用的脚本-
import-module ActiveDirectory
import-csv y:\d1.csv -Delimiter ";" | Foreach-Object {add-adgroupmember -Identity $_.group -Members $_.name}    

group;name
"CN=example,OU=example,DC=example,DC=example,DC=example";"CN=example,OU=example,OU=example,DC=example,DC=example,DC=example"
"CN=example-AE,OU=example,DC=example,DC=example,DC=example";"CN=example,OU=example,OU=example,DC=example,DC=example,DC=example"


我有2000个用户可以添加到大约100个群组中,也许有更好的方法吗?我在这里缺少简单的东西吗?

最佳答案

有时,此类问题是由输入数据文件中的意外数据(或缺少数据)引起的。我认为这是Get-Content而不是Import-Csv的问题,但是...

确保您没有从CSV传递空白数据(例如,文件底部的空白行等)。您可以尝试通过Where-Object检查来管道导入Import-Csv cmdlet的输出,以确保只有有效数据才能到达Foreach-Object循环:

Import-Csv y:\d1.csv -Delimiter ";" | 
  Where-Object{-not [String]::IsNullOrEmpty($_.group)} |
    Foreach-Object {Add-ADGroupMember -Identity $_.group -Members $_.name}

显然,您也可以对Name属性执行类似的检查。

关于powershell - 用Powershell add-adgroupmember结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105455/

相关文章:

azure - 如何为 Get-AzureADUserAppRoleAssignment 创建循环?

c++ - DsMakeSpn 在 Windows Server 2008 上总是失败

c# - 使用 Forefront Identity Manager 进行自助式密码重置有哪些选项?

用于组织、团队和用户管理的 Azure AD B2C 或 B2B

android - ADAL 可以与 AD FS 而不是 Azure AD 一起使用吗?

active-directory - Active Directory 中的 LDAP 过滤器字符串长度限制是多少?

PowerShell 脚本,用于将超过 x 天的文件和文件夹(包括子文件夹)从一个位置移动到另一个位置

powershell - 位置参数之前的动态开关可能无法按预期工作

c# - PowerShell - 枚举类型的参数值选项卡扩展

arrays - 组合 2 个 PowerShell 数组