从一个导入 csv 将多个用户添加到多个组(后续查询) [英] Add multiple users to multiple groups from one import csv (follow up query)

查看:25
本文介绍了从一个导入 csv 将多个用户添加到多个组(后续查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来使用多个用户名填充多个通讯组.我遇到了 a本网站上的脚本由成员 Frode F 编写.:

I was looking for a way to populate multiple distribution groups with multiple user names. I came across a script on this site written by member Frode F.:

Import-Csv "C:ScriptsImport Bulk Users into bulk groupsulkgroups3.csv" | Group-Object Group | % {
    #Foreach Group, get ADUser object for users and add members
    $users = $_.Group | % { Get-ADUser $_.Accountname }
    Add-ADGroupMember -Identity $_.Name -Member $users
}

当 csv 文件包含大约 10 行、第 1 列中有 2 个不同的组和第 2 列中有多个用户时,这是有效的.当 csv 包含几百行时,仍然只有 2 个组,它根本无法填充这些组.这些是错误:

This was working when the csv file contained about 10 lines with 2 different groups in column 1 and multiple users in column 2. When the csv contains a few hundred lines, still only 2 groups it fails to populate the groups at all. These are the errors:

Add-ADGroupMember : The specified account name is already a member of the group
At C:scriptsAddUsersDistributionGroups.ps1:6 char:22
+     Add-ADGroupMember <<<<  -Identity $_.Name -Member $users
    + CategoryInfo          : NotSpecified: (Group A:ADGroup) [Add-ADGroupMember], ADException
    + FullyQualifiedErrorId : The specified account name is already a member of the group,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

Add-ADGroupMember : The specified account name is already a member of the group
At C:scriptsAddUsersDistributionGroups.ps1:6 char:22
+     Add-ADGroupMember <<<<  -Identity $_.Name -Member $users
    + CategoryInfo          : NotSpecified: (Group B:ADGroup) [Add-ADGroupMember], ADException
    + FullyQualifiedErrorId : The specified account name is already a member of the group,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

有人可以帮忙吗?

推荐答案

也许 Add-ADGroupMember 可以同时处理的成员数量有限制,如果您通过 -Members 参数数百个的集合?不要将所有用户分组到一个集合中,而是尝试一次添加一个:

Perhaps there's a limit to how many members Add-ADGroupMember can handle at once, and it croaks if you pass the -Members parameter a collection of hundreds? Instead of grouping all the users into a collection, try adding them one at a time:

Import-Csv "C:ScriptsImport Bulk Users into bulk groupsulkgroups3.csv" | %{
    Add-ADGroupMember -Identity $_.Group -Members $_.Accountname
}

这假设 GroupAccountname 列中的值是有效的身份属性 - 它们必须是为了您引用的脚本工作.但请注意,此脚本将需要很长时间才能运行,因为 AD cmdlet 通常执行速度很慢,并且您将为每个用户执行一次 Add-ADGroupMember(即,CSV 文件中的每一行)文件)而不是每组一次.您可能会发现在 Foreach-Object 块中添加一行指示进度很有用,以便您知道它正在工作而不是卡住.

This assumes that the values in the Group and Accountname columns are valid identity properties--which they'd have to be in order for the script you quoted to work. Note, though, that this script will take a long time to run, because the AD cmdlets usually execute slowly, and you'll be executing Add-ADGroupMember once per user (i.e., per line in the CSV file) rather than once per group. You might find it useful to add a line to the Foreach-Object block indicating progress so that you know it's working rather than getting stuck.

这篇关于从一个导入 csv 将多个用户添加到多个组(后续查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆