![](/img/trans.png)
[英]How do I get the ObjectID associated with the authenticated Azure service principal?
[英]How can I determine whether I am signed in with a user or service principal in Azure PowerShell and how do I get the ObjectId?
我想传递当前用户的ObjectId
,将部署脚本运行到部署步骤,例如将此当前用户添加到 Azure Key Vault 访问策略。
如何独立于用户或服务主体是否正在执行脚本来确定此ObjectId
?
Get-AzContext
可用于确定使用哪种类型的帐户,然后Get-AzADUser
或Get-AzADServicePrincipal
来获取ObjectId
:
function Get-CurrentAzUserObjectId {
$objectId = $null
$ctx = Get-AzContext
if ($ctx.Account.Type -eq "User") {
$u = Get-AzADUser -Mail $ctx.Account.Id
$objectId = $u.Id
}
else {
$sp = Get-AzADServicePrincipal -ApplicationId $ctx.Account.Id
$objectId = $sp.Id
}
return $objectId
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.