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

[玩转系统] 获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户

作者:精品下载站 日期:2024-12-14 03:42:11 浏览:15 分类:玩电脑

获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户


就像使用本地 Active Directory 一样,我们可以使用 PowerShell 管理 Azure AD 中的用户。为此,我们需要在 Powershell 中使用 Get AzureADUser cmdlet。此 cmdlet 是 PowerShell AzureAD 模块的一部分。

使用 Microsoft 365 时,您的用户实际上存储在 Azure Active Directory (Azure AD) 中。您可以通过 Azure 门户或 Microsoft 365 管理中心管理它们,但 PowerShell 速度要快得多。它可以让我们快速查找并导出用户信息。

在本文中,我们将了解获取 AzureADUser cmdlet。我将提供一些查找和导出用户信息的有用示例。在本文末尾,我有一个完整的脚本来导出 Azure AD 用户。

笔记

Azure AD 模块将于 2022 年底停止工作。我们需要切换到适用于 PowerShell 的 Microsoft Graph SDK。阅读本文,使用 Get-MgUser cmdlet 获取并导出 Azure AD 用户。

在 PowerShell 中使用 Get-AzureAD 查找 Azure AD 用户

在开始之前,请确保已安装 Azure AD 模块。

Get-AzureADUser cmdlet 允许从 Azure Active Directory 查找并提取用户帐户。该 cmdlet 仅附带几个我们可以使用的参数:

  • 过滤 - 基于 oDate v3 查询检索多个对象
  • ObjectId - 根据 UPN 或 ObjectID 返回特定用户
  • SearchString - 获取与searchString匹配的所有用户
  • 全部 - 检索所有结果
  • 顶部 - 仅检索指定金额

建议

默认情况下,Get-AzureADUser cmdlet 仅返回 100 条记录。在-all $true参数处获取所有结果

要在 Azure AD 中查找单个用户,我们只需使用 ObjectID,它接受 UserPrincipalName 作为值。

Get-AzureADUser -ObjectId [email protected]

[玩转系统] 获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户

默认情况下,Get-AzureADUser cmdlet 仅返回四个字段。如果你想查看用户的所有属性,那么你可以简单地在add后面添加select *

Get-AzureADUser -ObjectId [email protected] | select *

我将在本文后面详细解释这些属性。

在 AzureADUser 中使用搜索字符串

searchString 参数是一个有趣的参数。根据文档,searchstring 参数仅搜索 DisplayName 或 UserPrincipalName 中的第一个字符。

但在测试 cmdlet 时,我注意到它搜索了更多字段:

  • UserPrincipalName - 字符串的第一部分
  • DisplayName - 字符串的第一部分
  • 职位名称 - 完全匹配
  • 部门 - 完全匹配
  • 城市 - 完全匹配
  • 状态 - 完全匹配
  • 国家/地区 - 完全匹配

因此,searchString 参数可用于搜索用户的全名或名称的第一部分。对于其他字段,您将需要搜索确切的值。

下面您可以看到我的开发租户中的一位用户的屏幕截图。我已重命名用户的名字和姓氏字段。另外,请注意我设置的唯一的部门名称。

[玩转系统] 获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户

现在,我们将使用 Get-AzureADUsers searchString cmdlet 以所有可能的方式查找用户 Alex Wilber。

首先,我们搜索显示名称的第一部分:

Get-AzureADUser -SearchString "Alex"

ObjectId                       DisplayName UserPrincipalName             UserType
--------                       ----------- -----                         --------
449d2fd4-8165-415f-b4f1-       Alex Wilber [email protected] Member

如果我们尝试搜索名字“Alexed”或姓氏“Wilbers”,那么搜索字符串将不起作用:

# Search on Firstname
Get-AzureADUser -SearchString "Alexed"

---
# No results

# Search on Lastname
Get-AzureADUser -SearchString "Wilber"

---
# No results

在其他字段上使用 searchString

所有其他字段都需要完全匹配。亚历克斯的职位是营销助理。如果我们只搜索职位名称“marketing”的第一部分,那么我们将不会得到预期的结果:

Get-AzureADUser -SearchString "Marketing" | Select DisplayName,UserPrincipalName,jobtitle,department

DisplayName UserPrincipalName              JobTitle  Department
----------- -----------------              --------  ----------
Megan Bowen [email protected] recruiter Marketing

它返回梅根·鲍文,因为她在“营销”部门工作。当搜索 Alex 的整个职位名称时,我们得到了预期的结果:

Get-AzureADUser -SearchString "Marketing Assistant" | Select DisplayName,UserPrincipalName,jobtitle,department

DisplayName UserPrincipalName             JobTitle            Department
----------- -----------------             --------            ----------
Alex Wilber [email protected] Marketing Assistant MarketingDepartment

我们可以对其他字段(城市、州和国家)使用相同的原则。

获取 AzureAD 用户筛选器

因此,searchString 参数非常适合通过名字快速查找 Azure AD 用户,但对于其他数据,它并不准确。查找 AzureAD 用户的更可靠方法是使用 -filter 参数。

过滤器查询基于 oDate v3 过滤器语句,当您不习惯时,正确执行可能会有点困难。就我个人而言,我发现 Get-ADUser cmdlet 使用的 PowerShell 表达式语言更易于使用。

Data v3 查询的棘手之处在于并非所有字段都支持所有运算符。例如,我们可以搜索职位名称为“营销助理”的所有用户

Get-AzureADUser -Filter "jobtitle eq 'Marketing Assistant'"

[玩转系统] 获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户

这将使所有用户的职位名称等于“营销助理”。但我期望的是,我们也可以使用 ne (不等于)来获取所有非营销助理的用户。

Get-AzureADUser -Filter "jobtitle ne 'Marketing Assistant'"

[玩转系统] 获取 AzureADUser - 如何使用 PowerShell 查找 Azure AD 用户

但该运算符不受支持。因此,目前“获取 AzureADUser”筛选器参数仅支持以下运算符:

OperatorDescriptionExampleeqEquals tojobtitle eq ‘Marketing Assistant’andAndjobtitle eq ‘Recruiter’ and jobtitle eq ‘hr’ orOrjobtitle eq ‘Recruiter’ or jobtitle eq ‘hr’ startswithString starts withstartswith(jobtitle,’recr’)

Get-AzureADUser 筛选器示例

因此,让我们看一下在 Get-AzureADUser cmdlet 上使用筛选器参数的几个示例:

按全名过滤

Get-AzureADUser -Filter "DisplayName eq 'Adele Vance'"

按姓氏过滤

Get-AzureADUser -Filter "Surname eq 'Vance'"

搜索名称的第一部分

Get-AzureADUser -Filter "startswith(DisplayName,'Ade')"

根据用户类型过滤

请注意,我在此处添加了 -all 参数,因为我们预计结果会超过 100 个

# Find all users
Get-AzureADUser -Filter "UserType eq 'Member'" -All $true

# Find all guests
Get-AzureADUser -Filter "UserType eq 'Guest'" -All $true

禁用 Azure AD 帐户

请注意,我在这里添加了 -all 参数,因为我们预计结果会超过 100 个

Get-AzureADUser -Filter "accountEnabled eq false" -All $true

使用多个条件

Get-AzureADUser -Filter "department eq 'Marketing' and jobtitle eq 'Manager'"

根据其他字段查找Azure AD用户

使用 -filter-searchstring 参数时,搜索在服务器上完成,仅返回过滤后的结果。另一种选择是首先从 Azure AD 请求所有用户,然后在 PowerShell 中本地进行筛选。

例如,当我们想要搜索部分用户名时,我们可以执行以下操作:

Get-AzureADUser -All $true | Where-Object {$_.DisplayName -like "*vanc*"}

您可以在 Get-AzureADUser cmdlet 返回的所有数据上使用它,这也允许我们使用不等于运算符:

Get-AzureADUser -All $true | Where-Object {$_.jobtitle -ne "Marketing Assistant"} | ft

根据OU获取AzureADUser

我们还可以使用此原则来仅获取来自特定组织单位的用户。 OU 的 DistinguishedName 存储在 Get-AzureADUser 结果的扩展属性 onPremisesDistinguishedName 中。

要过滤 OU 上的用户,我们首先获取所有用户,然后仅选择可分辨名称与 like 表达式匹配的用户:

# Get all users from the OU Test-Users:
Get-AzureADUser -All $true | Where-Object {$_.ExtensionProperty.onPremisesDistinguishedName -like "*OU=Test-Users*"}

获取 AzureAD 用户属性

默认情况下,AzureAD User cmdlet 仅显示用户的四个字段,这并没有为我们提供很多信息。但实际返回的用户还有很多信息。如果选择单个用户并使用格式列表输出,您将看到该用户的所有数据。

Get-AzureADUser -ObjectId [email protected] | fl

您可以通过在其后面传递 select cmdlet 来简单地选择所需的字段:

Get-AzureADUser -ObjectId [email protected] | Select DisplayName, Jobtitle, Mail, Department

将 Azure AD 用户导出到 CSV

我创建了一个完整的脚本,它将具有最重要属性的所有 Azure AD 用户导出到 CSV 文件。该脚本还收集用户的管理员,您可以选择收集启用和/或禁用的用户帐户。

您可以在我的 Github 上找到完整的脚本,或者从下面复制粘贴它。

param(
  [Parameter(
    Mandatory = $false,
    HelpMessage = "Get the users manager"
  )]
  [switch]$getManager = $true,

  [Parameter(
    Mandatory = $false,
    HelpMessage = "Get accounts that are enabled, disabled or both"
  )]
    [ValidateSet("true", "false", "both")]
  [string]$enabled = "true",

  [Parameter(
    Mandatory = $false,
    HelpMessage = "Enter path to save the CSV file"
  )]
  [string]$path = ".\ADUsers-$((Get-Date -format "MMM-dd-yyyy").ToString()).csv"
)

$ErrorActionPreference = "Stop"

Function Get-Users {
    <#
    .SYNOPSIS
      Get users from the requested DN
    #>
    process{
      # Set the properties to retrieve
      $properties = @(
        'ObjectId',
        'DisplayName',
        'userprincipalname',
        'mail',
        'jobtitle',
        'department',
        'telephoneNumber',
        'PhysicalDeliveryOfficeName',
        'mobile',
        'streetAddress',
        'city',
        'postalcode',
        'state',
        'country',
        'AccountEnabled'
      )

      # Get enabled, disabled or both users
      switch ($enabled)
      {
        "true" {$filter = "AccountEnabled eq true"}
        "false" {$filter = "AccountEnabled eq false"}
        "both" {$filter = ""}
      }

      # Get the users
      Get-AzureADUser -Filter $filter | select $properties
    }
}


Function Get-AllAzureADUsers {
  <#
    .SYNOPSIS
      Get all AD users
  #>
  process {
    Write-Host "Collecting users" -ForegroundColor Cyan
    $users = @()

    # Collect users
    $users += Get-Users

    # Loop through all users
    $users | ForEach {

      $manager = ""

      If (($getManager.IsPresent)) {
        # Get the users' manager
        $manager = Get-AzureADUserManager -ObjectId $_.ObjectId | Select -ExpandProperty DisplayName
      }

      [pscustomobject]@{
        "Name" = $_.DisplayName
        "UserPrincipalName" = $_.UserPrincipalName
        "Emailaddress" = $_.mail
        "Job title" = $_.JobTitle
        "Manager" = $manager
        "Department" = $_.Department
        "Office" = $_.PhysicalDeliveryOfficeName
        "Phone" = $_.telephoneNumber
        "Mobile" = $_.mobile
        "Enabled" = if ($_.AccountEnabled) {"enabled"} else {"disabled"}
        "Street" = $_.StreetAddress
        "City" = $_.City
        "Postal code" = $_.PostalCode
        "State" = $_.State
        "Country" = $_.Country
      }
    }
  }
}

Get-AllAzureADUsers | Sort-Object Name | Export-CSV -Path $path -NoTypeInformation

if ((Get-Item $path).Length -gt 0) {
  Write-Host "Report finished and saved in $path" -ForegroundColor Green

  # Open the CSV file
  Invoke-Item $path

}else{
  Write-Host "Failed to create report" -ForegroundColor Red
}

总结

Get AzureADUser cmdlet 与 Get-ADUser cmdlet 有很大不同。过滤用户有点困难,但您始终可以检索所有用户帐户并在 PowerShell 中进行过滤。

请记住,默认情况下,Get-AzureADUser cmdlet 仅返回 100 条记录。因此,当您期望更多结果时,请添加 -all 参数。

我希望这篇文章对您有用,如果您有任何疑问,请在下面发表评论。

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

取消回复欢迎 发表评论:

关灯