当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:获取和导出 Azure AD 用户

作者:精品下载站 日期:2024-12-15 00:38:13 浏览:14 分类:玩电脑

PowerShell:获取和导出 Azure AD 用户


在本指南中,您将了解如何使用 PowerShell 获取 Azure AD 用户、所有用户属性并将其导出到 CSV 文件。

作为管理员,您可能需要查看 Azure AD 用户及其相关属性的列表。这对于安全审核、帐户配置审查或稍后在脚本中使用非常有用。

要获取 Azure 用户列表,可以使用 PowerShell Get-MgUser 命令。使用此命令,您可以获取所有 Azure 用户帐户、搜索特定帐户以及显示所有或特定用户属性。

重要

Microsoft 将在 2023 年 6 月 30 日之后随时停用 Azure AD Graph 和 MSonline API。其中包括多年来用于为 Azure 用户提供 PowerShell 的 Get-AzureADUser cmdlet。

这些 API 正在被 Microsoft Graph API 取代。在本指南中,我将使用新支持的 Get-MgUser cmdlet 来获取 Azure AD 用户。有关更多详细信息,请参阅 Microsoft Entra 更改公告。

让我们开始吧。

要求

要遵循本指南,您将需要以下内容:

  • 安装了 PowerShell 5.1 或更高版本 - 如何更新 PowerShell
  • 安装了 Net Framework 4.7.2 或更高版本
  • 安装 Microsoft Graph 模块 - 步骤如下

如何使用 PowerShell 获取 Azure AD 用户属性

在第一个示例中,我将向您展示如何使用 get-mguser 命令获取所有 Azure AD 用户的列表。

步骤 1. 安装 Microsoft Graph PowerShell SDK

您可以将 Microsoft Graph 模块安装到当前用户或所有用户。

要为当前用户安装模块,请运行此命令。

Install-Module Microsoft.Graph -Scope CurrentUser

为计算机上的所有用户安装该模块。

Install-Module Microsoft.Graph -Scope AllUsers

在此示例中,我将为当前用户安装该模块。

可能会提示您正在从不受信任的存储库安装模块。

键入 y 从不受信任的存储库安装。

[玩转系统] PowerShell:获取和导出 Azure AD 用户

您应该看到正在安装各种软件包。完成后,它应该返回到 PowerShell 提示符。

要验证图形模块是否已安装,请运行以下命令。

find-module microsoft.graph

[玩转系统] PowerShell:获取和导出 Azure AD 用户

安装完成。

步骤 2. 使用 PowerShell 连接到 Microsoft Graph

在使用 PowerShell 获取 Azure 用户之前,您首先需要使用 -scope 参数和正确的权限进行连接。请参阅 Microsoft 文章 Microsoft Graph 权限参考以查看所有权限。

以下命令将允许您读取完整的 Azure 用户配置文件属性集。

Connect-MgGraph -Scopes "User.Read.All"

当您运行上述命令时,系统将提示您登录。

[玩转系统] PowerShell:获取和导出 Azure AD 用户

使用您的帐户登录以完成与 Microsoft Graph 的连接。

步骤 3. 使用 Get-MgUser 获取 Azure AD 用户

如果您按照步骤 1 和 2 进行操作,您应该连接到 Microsoft Graph 并且无法运行 get-MgUser cmdlet。

要让所有 Azure 用户运行此命令。

get-mguser -all

此命令将返回用户 Id、DisplayName、Mail 和 UserPrincipalName 属性。

[玩转系统] PowerShell:获取和导出 Azure AD 用户

要获取单个 Azure 用户,请使用 -UserID 参数。

get-mguser -UserID "[email protected]"

[玩转系统] PowerShell:获取和导出 Azure AD 用户

Get-MgUser 示例

下面是有关如何使用 Get-MGuser cmdlet 获取 Azure 用户的更多示例。

1. 搜索特定的 Azure 用户

在此示例中,我将使用 -Filter 参数来搜索具有特定显示名称的用户。

 Get-MgUser -Filter "startsWith(DisplayName, 'Robert')"

[玩转系统] PowerShell:获取和导出 Azure AD 用户

2.获取所有启用的Azure用户

在此示例中,我将搜索并仅获取已启用的 Azure AD 用户。

Get-MgUser -Filter 'accountEnabled eq true' -All -property AccountEnabled, DisplayName | select DisplayName, AccountEnabled

[玩转系统] PowerShell:获取和导出 Azure AD 用户

3.显示所有Azure用户属性

要显示用户的所有属性,请使用 fl 参数。

get-mguser -UserID "[email protected]" | fl

[玩转系统] PowerShell:获取和导出 Azure AD 用户

正如您在上面的屏幕截图中看到的,大多数用户属性都是空白的。这是设计使然,大多数属性要求您指定要显示的属性数据。有关详细信息,请参阅下一个示例。

4. 显示特定用户属性

要显示默认情况下不显示的用户属性,请使用 select 参数。

在此示例中,我将显示用户的创建日期时间。

get-mguser -UserID "[email protected]" -Property CreatedDateTime | select CreatedDateTime

[玩转系统] PowerShell:获取和导出 Azure AD 用户

使用 select 参数时,您需要列出要显示的每个属性。这是包含显示名称的另一个示例。

get-mguser -UserID "[email protected]" -Property CreatedDateTime, displayname | select CreatedDateTime, displayname

要查看默认显示哪些属性以及需要使用 select 的属性,请参阅本文末尾的表格。

5. 按部门搜索用户

在此示例中,我将搜索“会计”部门的所有用户。

Get-MgUser -Filter "Department eq 'Accounting'"

[玩转系统] PowerShell:获取和导出 Azure AD 用户

要仅显示 DisplayName 和 Department 属性,请使用此命令。

Get-MgUser -Filter "Department eq 'Accounting'" -property displayname, department | select displayname, department

6. 扩展用户属性

当您显示完整的属性列表时,您会注意到有些属性显示为“Microsoft.Graph.PowerShell.Models”。

[玩转系统] PowerShell:获取和导出 Azure AD 用户

其中一些属性可以轻松扩展以查看相关数据。问题是不是所有的都可以扩展。有关支持扩展的属性的完整列表,请参阅 Azure 用户资源类型文档。

向下滚动到关系并查找显示支持 $expand 的属性。

[玩转系统] PowerShell:获取和导出 Azure AD 用户

在上面的屏幕截图中,您可以看到管理器属性支持扩展。

下面是扩展 manager 属性的示例。

Get-MgUser -UserId [email protected] -ExpandProperty manager | Select @{Name = 'Manager'; Expression = {$_.Manager.AdditionalProperties.displayName}}

[玩转系统] PowerShell:获取和导出 Azure AD 用户

7. 将 Azure 用户导出到 CSV

要将 Azure 用户导出到 CSV,请使用 export-csv 命令

get-mguser -UserID "[email protected]" | export-csv c:\it\azure-1.csv -NoTypeInformation

8. 获取所有用户的创建日期时间

在此示例中,我将显示所有用户的 DisplayName 和创建日期时间。

get-mguser -all -Property displayName, CreatedDateTime | select displayName,CreatedDateTime

[玩转系统] PowerShell:获取和导出 Azure AD 用户

Azure 用户属性列表

下面是 Get-MgUser cmdlet 返回的所有用户属性的列表,总共 125 个。

仅在 $select 上返回意味着您必须使用 select 参数,后跟属性名称才能显示任何用户数据。详细信息请参见示例 4。

aboutMe

真的

accountEnabled

真的

Activities

不适用

ageGroup

真的

AgreementAcceptances

不适用

AppRoleAssignments

不适用

assignedLicences

真的

assignedPlans

真的

Authentication

AuthorizationInfo

birthday

真的

businessPhones

False - 默认返回

Calendar

CalendarGroups

CalendarView

Calendars

Chats

city

真的

companyName

真的

consentProvidedForMinor

真的

ContactFolders

Contacts

county

真的

createdDateTime

真的

CreatedObjects

creationType

真的

deletedDateTime

真的

department

真的

DeviceEnrollmentLimit

DeviceManagementTroubleshootingEvents

DirectReports

displayName

False - 默认返回

Drive

Drives

employeeHireDate

真的

employeeLeaveDateTime

False - 默认返回

employeeId

真的

employeeOrgData

真的

employeeType

真的

Events

Extensions

externalUserState

真的

externalUserStateChangeDateTime

真的

faxNumber

真的

FollowedSites

givenName

False - 默认返回

hireDate

真的

id

False - 默认返回

identities

真的

imAddresses

真的

InferenceClassification

Insights

interests

真的

isResourceAccount

不适用

jobTitle

False - 默认返回

JoinedTeams

lastPasswordChangeDateTime

真的

legalAgeGroupClassification

真的

licenseAssignmentStates

真的

LicenseDetails

mail

False - 默认返回

MailFolders

mailboxSettings

真的

mailNickname

真的

ManagedAppRegistrations

ManagedDevices

Manager

MemberOf

Messages

mobilePhone

False - 默认返回

mySite

真的

mySiteOauth2PermissionGrants

officeLocation

False - 默认返回

onPremisesDistinguishedName

真的

onPremisesDomainName

真的

onPremisesExtensionAttributes

真的

onPremisesImmutableId

真的

onPremisesLastSyncDateTime

真的

onPremisesProvisioningErrors

真的

onPremisesSamAccountName

真的

onPremisesSecurityIdentifier

真的

onPremisesSyncEnabled

真的

onPremisesUserPrincipalName

真的

Onenote

OnlineMeetings

otherMails

真的

Outlook

OwnedDevices

OwnedObjects

passwordPolicies

真的

passwordProfile

真的

pastProjects

真的

People

Photo

Photos

Planner

postalCode

真的

preferredDataLocation

不适用

preferredLanguage

False - 默认返回

preferredName

真的

Presence

provisionedPlans

真的

proxyAddresses

真的

RegisteredDevices

responsibilities

真的

schools

真的

ScopedRoleMemberOf

securityIdentifier

False - 默认返回

Settings

showInAddressList

不适用

signInSessionsValidFromDateTime

真的

skills

真的

state

真的

streetAddress

真的

surname

False - 默认返回

Teamwork

Todo

TransitiveMemberOf

usageLocation

真的

userPrincipalName

False - 默认返回

userType

真的

AdditionalProperties

概括

在本指南中,我演练了如何使用 PowerShell 获取 Azure AD 用户。由于 Microsoft 停用了 Get-AzureADUser cmdlet,我专注于使用新支持的 Get-MgUser PowerShell 命令。

在我看来,微软让从 Azure 获取用户变得更加复杂。要获取所有用户属性,您必须使用展开选项并查找相关命令来展开某些属性。也许这有一个很好的理由,我不知道。

此外,终止支持和更换命令会让情况变得更加令人沮丧。遗憾的是,微软没有让 Azure cmdlet 保持简单,就像用于本地 ADDS 的 PowerShell get-aduser 命令一样。

资源

  • Azure AD 用户属性
  • Microsoft Graph 权限参考

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯