多个用户添加到多个组从一个导入CSV(跟踪查询)多个、用户、CSV

2023-09-08 12:25:52 作者:鸡,你的鼻子有两根香肠~

我一直在寻找一种方式来填充多个通讯组,多个用户名。我碰到a本网站上所写的成员弗罗德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:\Scripts\Import Bulk Users into bulk groups\bulkgroups3.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文件包含在列1 2个不同的组和多个用户在第2栏10行。 当的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:\scripts\AddUsersDistributionGroups.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:\scripts\AddUsersDistributionGroups.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

谁能帮助?

推荐答案

也许有一个限制,许多成员如何添加-ADGroupMember 可以处理一次,并croaks如果你通过在 - 成员参数数以百计的集合?相反,所有的用户分组到一个集合中,尝试将他们一次一个:

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:\Scripts\Import Bulk Users into bulk groups\bulkgroups3.csv" | %{
    Add-ADGroupMember -Identity $_.Group -Members $_.Accountname
}

这假定在组的值和帐户名列的有效身份证性能 - 他们不得不是为了让你报工作的脚本。但是请注意,这个脚本会需要很长的时间来运行,因为AD cmdlet的通常运行慢,你会被执行添加-ADGroupMember 一次,每个用户(即每行的CSV文件),而不是一次每个组。你可能会发现它有用添加一行到的foreach对象块指示,让你知道它的工作,而不是被卡住的进步。

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.