如何使用 powershell 从 CSV 文件创建 M365 动态组

How to create M365 dynamic groups from CSV file using powershell

我的任务是为我们的 Azure AD 部署创建 1600 个 O365 动态组。我们目前在 CSV 文件中为这些组创建了所有 syntax/group 规则。我的问题是:我正在使用以下脚本尝试创建这些组

Connect-AzureAD
$Groups = Import-Csv -Path C:\Temp\AzureAD_Groups.csv
$dynamicGroupTypeString = "DynamicMembership"

foreach($Group in $Groups)
{
New-AzureADGroup -DisplayName $Group.DisplayName -Description $Group.Description -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -membershipRule $Group.MembershipRule -GroupTypes $dynamicGroupTypeString
} 

在 运行 这个脚本之后我得到错误:

New-AzureADGroup : A parameter cannot be found that matches parameter name 'membershipRule'.
At line:8 char:147
+ ...  -MailNickName "group" -SecurityEnabled $True -membershipRule $Group. ...

我调查了这个错误,我发现这个 flag/parameter 对于 New-AzureADGroup 不存在,我还看到它适用于 Set-AzureADGroup。我的问题是:我可以在一个脚本中全部执行此任务,以便它创建组然后在我的 CSV 中使用预制语法,而不是创建然后设置组规则吗?

我的 CSV 文件是这样设置的:

非常感谢任何帮助,谢谢。

我试图在我的环境中重现相同的内容,但得到了如下相同的错误:

我导入的 CSV 文件 如下所示:

Please note that, you have to use New-AzureADMSGroup command to create M365 Dynamic group. Make sure to include -MembershipRuleProcessingState "On" while using that command.

像下面这样更改脚本后,我能够成功创建动态组:

Connect-AzureAD
$Groups = Import-Csv -Path "C:\test\AzureAD_groups.csv"
$dynamicGroupTypeString = "DynamicMembership"

foreach($Group in $Groups)
{
New-AzureADMSGroup -DisplayName $Group.DisplayName -Description $Group.Description -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -membershipRule $Group.MembershipRule -GroupTypes $dynamicGroupTypeString -MembershipRuleProcessingState "On"
} 

M365动态组创建成功如下:

成员根据如下成员资格规则动态分配到组: