Azure服务主体添加name=value标签遇MissingSubscription错误求助
解决Azure Service Principal添加标签的问题
你遇到的错误是因为Set-AzResource仅适用于Azure资源管理器(ARM)订阅范围内的资源(如虚拟机、存储账户等),而Service Principal是Azure AD的租户级对象,不属于任何订阅,因此该命令无法对其操作。以下是正确的操作方法:
PowerShell 操作步骤
- 获取目标Service Principal对象:
$sp = Get-AzADServicePrincipal -DisplayName "tempdelete"
准备标签(Azure AD的Service Principal标签为字符串数组,每个元素格式为
key=value):- 若要覆盖所有现有标签:
$tags = @("name=value")- 若要保留现有标签并添加新标签:
$existingTags = $sp.Tags $newTags = $existingTags + "name=value"执行标签更新:
Update-AzADServicePrincipal -ObjectId $sp.Id -Tags $newTags
Azure CLI 操作步骤
使用你提到的命令即可直接添加标签,支持单次添加或多次添加:
- 单次添加标签:
az ad sp update --id <service_principal_id> --add tags "name=value"
- 添加多个标签:
az ad sp update --id <service_principal_id> --add tags "name1=value1" --add tags "name2=value2"
验证标签是否生效
执行以下命令查看Service Principal的标签:
Get-AzADServicePrincipal -ObjectId $sp.Id | Select-Object DisplayName, Tags
内容的提问来源于stack exchange,提问作者not_a_generic_user
相关产品推荐
相关产品推荐

