azure AD powerShell 编辑管理器

azure AD powerShell edit manager

我编写了一个 DotNet Forms 应用程序,它使用 PowerShell 自动化在 On-premise AD、On-premise Exchange、Azure AD 和 O365 中创建和修改用户,以匹配 HR 提供的记录。这已经被客户使用了几年并且工作正常。

该代码使用 Windows PowerShell (MSOnline - MSOL) 的 Azure Active Directory 模块来查看和编辑 Azure AD 中的用户。我最初使用的是 MSOL 版本 8073.4,但我已经升级到 MSOL 版本 1.1.166.0 (参见 http://social.technet.microsoft.com/wiki/contents/articles/28552.microsoft-azure-active-directory-powershell-module-version-release-history.aspx

例如,我将使用以下 PowerShell 修改用户的标题:

Import-Module MSOnline
$Cred = Get-Credential
Connect-MSOLService -Credential $Cred
Set-MSOLUser -UserPrincipalName Santa@northpole.com -Title 'Deliverer of presents'

一切都很好,直到我被要求扩展代码以更新每个 Azure AD 用户的 "Manager ID" 属性。我想很容易!我只需要像更新标题一样更新用户的 "Manager ID" 字段(这是经理的 Azure AD 帐户的 ObjectID).....

呃,不。我找不到任何方法来更改经理字段。我翻遍了 MSDN 文档,但找不到任何方法来执行此操作:

所以我查看了目前处于预览状态的新 v2 Azure AD 模块(在上面的发布历史中提到 URL)并且可以从 PowerShell Gallery 下载(搜索 "AzureADPreview"). 这些最终将取代旧的 MSOL cmdlet,并且看起来与现有的 Azure PowerShell 模块(用于创建 VM 等)非常相似。 这确实支持通过命令

设置用户的 "manager ID"
Set-AzureADUserManager

我已经尝试过了并且它有效,所以我想我应该更新我的应用程序以使用新的 v2 APIs 而不是 v1 APIs (MSOL)。

不幸的是我发现

Set-AzureADUser

命令(用于设置职务等属性)在 v2.0.0.1 中完全损坏并且失败并显示错误

"Exception has been thrown by the target of the invocation"

对于我尝试的任何组合。我已通过 PowerShell 库将此报告给开发人员。

幸运的是,我发现这些模块的先前版本 1.1.167.0 工作正常,所以我正在使用该版本,现在可以成功创建用户、修改用户、配置用户 "Manager ID" 但我无法解决如何设置许可证(例如 O365_BUSINESS_PREMIUM)。命令 Set-AzureADUserLicense 的文档几乎 non-existent 而我一直无法弄清楚如何使用它。

我想我需要做以下事情:

# Create an object which contains the individual license 'x' I want to add
# The available license SkuIDs can be read from Get-AzureADSubscribedSku
$MySingleLicenseToAdd = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$MySingleLicenseToAdd.SkuID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

# Create a licenses object which is assigned the individual licenses I want to add or remove
$MyLicensesToAddOrRemove = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$MyLicensesToAddOrRemove.AddLicenses = $MySingleLicenseToAdd
$MyLicensesToAddOrRemove.RemoveLicenses = $Null

# Perform the license action against the specified user 'y'
Set-AzureADUserLicense -ObjectId 'yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyy' -AssignedLicenses $MyLicensesToAddOrRemove

但它在第二行代码中失败,表示 "SkuID" 是一个 read-only 字段。

所以我无法使用 V1 (MSOL) APIs,因为我找不到更新用户的 "Manager ID" 字段的方法。 我不能使用 V2 APIs,因为我找不到分配许可证的方法(而且它处于预览状态,所以在现场使用不是个好主意)

我目前的计划是重新使用 V1 APIs,然后使用 V2 APIs 仅更新 "Manager ID" 字段,但这很难一个理想的解决方案(因为我将使用两个不同的 API 登录两次 Azure)所以我想知道是否有人可以提供任何建议?

  1. 我的偏好是使用 v1 (MSOL) APIs 来更新 "Manager ID" 字段。
  2. 我的第二个偏好是使用 v2 APIs 并了解如何分配许可证。
  3. 我的第三个偏好是其他任何东西 ;)

我已经阅读了一篇关于直接使用 REST API 的文章,但那太繁琐了,如果可能的话,我宁愿避免并坚持使用 Azure PowerShell API。

抱歉这个太长的问题,但我试图提供一些背景来说明我为什么要尝试使用 V2 APIs。

更新 (23/09/2016):

AzureADPreview 2.0.0.2 刚刚发布,它修复了 Set-AzureADUser 的问题 :) 但不幸的是部分破坏了 Set-AzureADUserManager :(

这个新版本的许可证有同样的问题

下面是一个示例,说明如何使用 Set-AzureADuserLicense cmdlet 为用户设置许可证。

如果这说明了,请告诉我。

# Get the License SkuId from a template user that we want to apply to the new user 
$licensedUser = Get-AzureADUser -ObjectId "TemplateUser@contoso.com"  
# Get the new User we want to apply the license too 
$user = Get-AzureADUser -ObjectId "newuser@contoso.com"  
# Create the new License object 
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense 
$license.SkuId = $licensedUser.AssignedLicenses.SkuId 
# Create the Licenses Table and add the license from above 
$licenses = New-Object -TypeName   Microsoft.Open.AzureAD.Model.AssignedLicenses 
$licenses.AddLicenses = $license 
# Apply the license to the new user 
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $licenses

感谢您的回复。抢

除了您从现有用户那里检索 SkuID 之外,您提供的代码与我尝试的代码相同(请参阅我原来问题中的代码)。

由于已经发布了两个新版本的 AzureADPreview(2.0.0.7 和 2.0.0.17),这促使我再次尝试使用新版本的 AzureADPreview 以及我最初发布时可用的原始版本。

我的结果如下:

2.0.0.1: Doesn't work. Read-Only error.

2.0.0.2: Doesn't work. Read-Only error.

2.0.0.7: Works

2.0.0.17: Works

所以基本上这是原始版本的 AzureADPreview 中的错误,但微软已经修复了它。

一切正常。