使用 PowerShell 更新在 Microsoft Docs 上找到的服务主体 ID: https://docs.microsoft.com/en-us/graph/api/resources/serviceprincipal?view=graph-rest-1.0#properties
服务主体 -> 属性 -> 标签
如何向服务主体添加自定义标签?
# Get all Service Principals
# az login
# az ad sp list --all
# Set Service Principal ID
$ServicePrincipalID = ""
# Show Service Principal Information
$ServicePrincipalData = az ad sp show --id $ServicePrincipalID
Write-Host $ServicePrincipalData
# Update Service Principal Properties Custom Tag
az rest --method PATCH --url https://graph.microsoft.com/v1.0/servicePrincipals/$ServicePrincipalID --body '{\"tags\":[\"AssetID\"]}'
# az rest --method PATCH --url https://graph.microsoft.com/v1.0/servicePrincipals/$ServicePrincipalID --body '{"tags":["AssetID","SubscriptionID"]}'
# https://docs.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-update
# az ad sp update --id $ServicePrincipalID --set tags=["AssetID_<>"]
# az ad sp update --id $ServicePrincipalID --set 'tags=["AssetID_<>"]'
# Custom JSON Object input for tags property
$input = @"
{
"tags": [
"AssetID_<>",
"SubscriptionID_<>"
],
}
"@
$jObject = $input | convertfrom-json
az ad sp update --id $ServicePrincipalID --set tags $jObject
答案1
要更新服务主体的标签,请调用 Microsoft Graph API更新 servicePrincipal:
$ az rest --method PATCH --url https://graph.microsoft.com/v1.0/servicePrincipals/52e3d1ac-48c1-4486-8ed6-ad99a74415a7 --body '{"tags":["mytag2_sp"]}'
$ az rest --method GET --url https://graph.microsoft.com/v1.0/servicePrincipals/52e3d1ac-48c1-4486-8ed6-ad99a74415a7
...
"tags": [
"mytag2_sp",
"mytag1_app"
],