开发者问题收集

带有 -UserAssignedIdentity 的 Set-AzApiManagement 给出错误请求

2021-02-19
496

我正在尝试从 Powershell 将用户分配的标识分配给 Azure 上的 API 管理实例。我已从以下链接获取参考 -

https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-use-managed-service-identity#code-try-7

我正在尝试相同的代码 -

# Get an API Management instance
$apimService = Get-AzApiManagement -ResourceGroupName $resourceGroupName -Name $apiManagementName

# Create a user-assigned identity. This requires installation of the "Az.ManagedServiceIdentity" module.
$userAssignedIdentity = New-AzUserAssignedIdentity -Name $userAssignedIdentityName -ResourceGroupName $resourceGroupName

# Update an API Management instance
$userIdentities = @($userAssignedIdentity.Id)
Set-AzApiManagement -InputObject $apimService -UserAssignedIdentity $userIdentities

但它总是给我以下错误消息 -

Set-AzApiManagement:操作返回无效状态代码“BadRequest”

我遗漏了什么?代码有什么问题?任何立即的帮助都非常感谢!!提前致谢.. 我正在使用 Powershell..

编辑

API 管理实例已经有一个用户分配标识,我正在尝试添加另一个..

1个回答

关于该问题,请参考以下代码

Connect-AzAccount

$userAssignedIdentity = New-AzUserAssignedIdentity -Name "myapi" -ResourceGroupName ""
$userIdentities = @($userAssignedIdentity.Id)

$apimService = Get-AzApiManagement -ResourceGroupName "" -Name "huryApim"

[string[]]$test= $apimService.Identity.UserAssignedIdentity.Keys
[string[]]$test += $userAssignedIdentity.Id

Set-AzApiManagement -InputObject $apimService -UserAssignedIdentity $test

enter image description here

Jim Xu
2021-02-22