【问题标题】:Add a new O365 users to AD group using Powershell使用 Powershell 将新的 O365 用户添加到 AD 组
【发布时间】:2020-07-10 06:48:32
【问题描述】:

我正在使用 Powershell 脚本将新用户添加到 O365 并分配多个许可证。但是,我现在正在尝试将新用户也添加到现有组中。

Powershell

Connect-MsolService -Credential $UserCredential
Import-Csv -Path "C:\Users\Jesse\Documents\Powershell\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuIdEMS,$_.AccountSkuIdENTERPRISEPACK} | Export-Csv -Path "C:\Users\Jesse\Documents\Powershell\NewAccountResults.csv" -Verbose

.CSV

DisplayName,FirstName,LastName,UserPrincipalName,Usagelocation,AccountSkuIdEMS, AccountSkuIdENTERPRISEPACK
Test Jesse,Test,Jesse,test.jesse@(Tenant).nl,NL,(Tenant):EMS,(Tenant):ENTERPRISEPACK

我找到了以下代码,但不知道如何将它正确地实现到我现有的代码中。我想我还需要连接到新服务才能使用此 cmdlet。是否可以在单个脚本中切换连接?

Add-UnifiedGroupLinks -Identity "Azure AD Join" -LinkType Members -Links test.jesse@(tenant).nl

【问题讨论】:

    标签: powershell azure-active-directory office365


    【解决方案1】:

    您可以使用 AzureAD 模块 cmd Add-AzureADGroupMember 将新用户添加到现有组。

    记得关注Azure Active Directory PowerShell for Graph来安装AzureAD模块。

    您可以在 Powershell 中为 AzureAD 模块重复使用 $Credential。它不需要您再次登录。

    这是我的示例供您参考:

    $Credential = Get-Credential
    
    Connect-MsolService -Credential $Credential
    
    Import-Csv -Path "E:\test\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation} | Export-Csv -Path "E:\test\NewAccountResults.csv"
    
    Connect-AzureAD -Credential $Credential
    
    $newusers = Import-Csv -Path "E:\test\NewAccounts.csv" | foreach {Get-AzureADUser -Filter "userPrincipalName eq '$($_.UserPrincipalName)'"}
    
    foreach ($user in $newusers){
        Add-AzureADGroupMember -ObjectId "{object id of the existing group}" -RefObjectId $user.ObjectId
    }
    

    顺便说一句,您可以从 Azure 门户获取现有组的对象 ID

    【讨论】:

    • 完美运行!非常感谢!如果我想一次向用户添加多个组。我该怎么办?
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2020-11-14
    相关资源
    最近更新 更多