通过 ImmutableId 在 Office 365 中获取用户

Get User in Office 365 by ImmutableId

  1. 我希望根据他的 ImmutableId 获取 Office 365 用户详细信息。但是,没有提供 ImmutableId 的直接属性,因此目前唯一的方法是在 Get-MSOLUser 的 where-object 中提供它。然而,这会遍历所有用户,因此不是一个好的解决方案。那还有没有别的办法呢?

  2. 我正在尝试构建一个工具来读取 On-Prem AD 中的用户并在 O365 上做一些许可工作。可能有多个 AD,因此我开始查看 ImmutableId 以唯一标识用户。使用 'ImmutableId' 是正确的方法吗?

However this would loop through all users and hence is not a good solution. So is there any other way?

ImmutableId 是在将本地 AD 与 Azure AD 同步时生成的。

ImmutableId = user.ObjectGUID.toBase64String()

$guid = [GUID]"{UserObjectId in on-premise AD}"

$bytearray = $guid.tobytearray()

$immutableID = [system.convert]::ToBase64String($bytearray)

ImmutableId主要用于AD同步,我们无法通过ImmutableId查询用户。

There could be multiple AD's and hence I started looking at ImmutableId to uniquely identify a user. Is using 'ImmutableId' the correct approach?

如果您使用 Microsoft Azure AD Sync 同步用户,您将在目录 "C:\Program Files\Microsoft Azure AD Sync\Data" 下找到数据库文件 "ADSync.mdf"。有一个名为 "mms_metaverse" 的 table,你可以在这里找到映射。

SELECT
[object_id],
[userPrincipalName],
[cloudAnchor] AS [CloudUserId],
[cloudSourceAnchor] AS [ImmutableId]
FROM
mms_metaverse

更新#1:

经过一些研究,我发现 Graph API 可以通过 ImmutableId 过滤用户。

https://graph.microsoft.com/v1.0/users?$filter=onPremisesImmutableId+eq+'zYGi36Y8tkCwX4lYBb8bUA=='

对于那些有兴趣将 C# 与 Microsoft.Graph 库结合使用来为使用 ImmutableID 的用户查询 MS Graph 的用户,这里是有关如何执行此操作的片段:

var filter = Uri.EscapeDataString($"onPremisesImmutableId eq '{immutableIdString}'");
var users = await graphClient.Users
    .Request()
    .Filter(filter)
    .Select("id")  // Add in whatever properties you need to extract here
    .GetAsync();

if (users.Count >= 1)
{
    // ... code ...
}
else
{
    Console.WriteLine($"No user with immutable ID {immutableIdString} was found in Azure AD");
}

请注意使用 Uri.EscapeDataString 来转义过滤器查询中的特殊字符。我遇到了 + 个角色乱丢东西的问题。

注意:我使用 System.DirectoryServices 模块执行内部搜索,并使用以下方法从 AD 对象的 GUID 中提取 ImmutableID:

Guid objectGuid = new Guid(objectGuidString);
var byteArray = objectGuid.ToByteArray();
string immutableId = Convert.ToBase64String(byteArray);

希望这对那里的人有所帮助。