【发布时间】:2016-11-13 15:56:47
【问题描述】:
我编写了一个 DotNet Forms 应用程序,它使用 PowerShell 自动化来创建和修改本地 AD、本地 Exchange、Azure AD 和 O365 中的用户,以匹配 HR 提供的记录。这已经被客户使用了几年并且运行良好。
代码使用适用于 Windows PowerShell 的 Azure Active Directory 模块 (MSOnline - MSOL) 在 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”属性之前,一切都很好。容易,我想!我只需要更新用户的“经理 ID”字段(即经理的 Azure AD 帐户的 ObjectID),就像我更新标题一样.....
呃,不。我找不到任何方法来更改经理字段。我翻遍了 MSDN 文档,找不到任何方法来做到这一点:
所以我查看了目前处于预览状态的新 v2 Azure AD 模块(在上面的发布历史 URL 中提到),可以从 PowerShell 库下载(搜索“AzureADPreview”)。 这些最终将取代旧的 MSOL cmdlet,并且看起来与现有的 Azure PowerShell 模块(用于创建 VM 等)非常相似。 这确实支持通过命令设置用户的“经理 ID”
Set-AzureADUserManager
我已经尝试过了,它确实有效,所以我想我应该更新我的应用程序以使用新的 v2 API 而不是 v1 API (MSOL)。
不幸的是,我发现了
Set-AzureADUser
命令(用于设置职位等属性)在 v2.0.0.1 中完全被破坏并失败并出现错误
“调用的目标已抛出异常”
对于我尝试的任何组合。我已通过 PowerShell 库向开发人员报告了这一情况。
幸运的是,我发现这些模块的先前版本 1.1.167.0 工作正常,所以我正在使用该版本,现在可以成功创建用户、修改用户、配置用户的“经理 ID”,但我不知道如何设置许可证(例如 O365_BUSINESS_PREMIUM)。 Set-AzureADUserLicense 命令的文档几乎不存在,我一直无法弄清楚如何使用它。
我认为我需要做以下事情:
# 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”是只读字段时失败。
所以我无法使用 V1 (MSOL) API,因为我找不到更新用户“经理 ID”字段的方法。 我无法使用 V2 API,因为我找不到分配许可证的方法(而且它处于预览阶段,因此在现场使用不是一个好主意)
我目前的计划是重新使用 V1 API,然后使用 V2 API 仅更新“Manager ID”字段,但这并不是一个理想的解决方案(因为我将登录到 Azure 两次有两个不同的 API)所以我想知道是否有人可以提供任何建议?
- 我的偏好是使用 v1 (MSOL) API 来更新 “经理 ID”字段。
- 我的第二个偏好是使用 v2 API 并学习如何分配许可证。
- 我的第三个偏好是其他任何东西 ;)
我读过一篇关于直接使用 REST API 的文章,但那篇文章太繁重了,如果可能的话,我宁愿避免并坚持使用 Azure PowerShell API。
很抱歉这个 looooong 问题,但我试图提供一些背景信息来说明我为什么要尝试使用 V2 API。
更新(2016 年 9 月 23 日):
AzureADPreview 2.0.0.2 刚刚发布,它解决了 Set-AzureADUser 的问题 :) 但不幸的是部分中断了 Set-AzureADUserManager :(
这个新版本的许可证存在同样的问题
【问题讨论】:
标签: powershell azure azure-powershell