如何使用Azure CLI通过对象ID获取Azure AD对象

13
在Azure门户中,可以根据对象ID查找Azure AD对象,如下所示:

enter image description here

是否可以使用Azure CLI通过对象ID检索Azure AD对象?

为了使用Azure CLI获取与对象ID相关的对象,似乎我需要事先知道相关资源是用户、组、设备、应用程序注册等,以便获取详细信息。例如,如果我知道对象ID是一个用户,则可以使用az ad user show --id。如果我只有对象ID,我不知道对象的“类型”,但某种方式,Portal可以找到这个!

虽然我更喜欢Azure CLI解决方案,但Azure PowerShell解决方案也比没有好。我提出这个问题是因为我正在尝试使用az keyvault list生成密钥保管库中访问策略的列表,但该CLI命令的访问策略列表仅显示每个策略的对象ID……我无法确定这些对象是用户、组等。

enter image description here

2个回答

20
如果您想通过对象 ID 获取 Azure AD 资源,我们可以使用以下 Microsoft Graph API。
POST https://graph.microsoft.com/v1.0/directoryObjects/getByIds
Content-type: application/json

{
    "ids":[""]
}

如果您想要使用Azure CLI调用Microsoft Graph,我们可以使用命令az rest

例如(我使用Azure云shell

az rest --method POST --url 'https://graph.microsoft.com/v1.0/directoryObjects/getByIds' --headers 'Content-Type=application/json'  --body '{\"ids\":[\"\"]}'

enter image description here

详细信息请参阅这里这里


5
谢谢。请注意,--body 字符串必须进行引号转义,否则调用会失败。例如:--body '{\"ids\":[\"087f593b-bedf-44d4-8732-96ab980f2b45\"]}'。看起来没有办法使用本地 PowerShell 实例进行相同的调用?也就是说,不能使用 Azure 云 Shell?我发现 Azure 云 Shell 对于编写长脚本而言非常不便。 - PoorInRichfield
这似乎在本地使用Azure CLI也可以运行,即不在Azure Cloud Shell中。 - PoorInRichfield

0
如果您有一个包含用户ID的CSV文件,这个脚本非常有用,可以一次查找所有用户。
param(
    $file = "query_data.csv"
)

$data = Get-Content $file | ConvertFrom-Csv

$userIds = $data.User | Get-Unique

$body = @{
    ids = $userIds
} | ConvertTo-Json -Compress;
$body = $body -replace '"', '\"';

$results = az rest `
--method POST `
--url 'https://graph.microsoft.com/v1.0/directoryObjects/getByIds' `
--headers 'Content-Type=application/json'  `
--body $body;

$results > results.json

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接