powershell - 包含员工 ID 和经理的 AzureAD 完整花名册报告

标签 powershell azure-powershell

我在优化此 PowerShell 脚本时遇到问题。目前需要 8 小时才能完成。我知道问题是由于 ForEach 公式而导致的 Get-AllAzureADUsers 函数。我想通过只需要它来查询存储在变量中的数据而不是我们租户中所有 80k 用户的系统来加快速度,但我正在为此苦苦挣扎。我对 powershell 还很陌生,但需要它每周提取报告,并且真的希望尝试缩短运行时间。

param (
    [string]$path = "C:\Temp\ADUsers-$((Get-Date -format "MM-dd-yyyy").ToString()).csv"

Function Get-Users{
   process {
      $properties = @(
         'ObjectId',
         '@{N="EmployeeId";E={_.ExtensionProperty["employeeId"]}',
         'DisplayName',
         'Surname',
         'givenName',
         'UserPrincipalName',
         'JobTitle',
         'CompanyName'
         '@{N="License":E={$_.ExtensionProperty["extension_a92a_msDS_cloudExtensionAttribute1"]}
       )
       Get-AzureADUser -Full $true -Filter 'accountEnabled eq true' | select $properties
    }
}

Function Get-AllAzureADUsers {
    process {
        $users = @()
        $users =+ Get-Users
        $users | ForEach {
             $manager = Get-AzureADUserManager -ObjectId $_.ObjectId | Select -ExpandProperty UserPrincipalName

             [pscustomobject]@{
             "Employee ID" = (Get-AzureADUser -ObjectID $_.ObjectId).ExtensionProperty["employeeId"]
             "First Name" = $_.surname
             "Last Name" = $_.givenName
             "Work Email" = $_.UserPrincipalName
             "Job Title" = $_.JobTitle
             "Department" = $_.CompanyName
             "Manager Email" = $manager
             "License" = (Get-AzureADUser -ObjectId $_.ObjectId).ExtensionProperty["extension_a92a_msDS_cloudExtensionAttribute1"]
             }
         }
     }
}
Get-AllAzureADUsers | Sort-Object Name | Export-CSV -Path $path -NoTypeInformation

最佳答案

似乎您的代码可以简化为:

param(
    [string] $path = "C:\Temp\ADUsers-$(Get-Date -format "MM-dd-yyyy").csv"
)

& {
    foreach($azuser in Get-AzureADUser -All $true -Filter 'accountEnabled eq true') {
        [pscustomobject]@{
            "Employee ID"   = $azuser.ExtensionProperty["employeeId"]
            "First Name"    = $azuser.surname
            "Last Name"     = $azuser.givenName
            "Work Email"    = $azuser.UserPrincipalName
            "Job Title"     = $azuser.JobTitle
            "Department"    = $azuser.CompanyName
            "Manager Email" = (Get-AzureADUserManager -ObjectId $azuser.ObjectId).UserPrincipalName
            "License"       = $azuser.ExtensionProperty["extension_a92a_msDS_cloudExtensionAttribute1"]
        }
    }
} | Export-CSV -Path $path -NoTypeInformation

花费这么长时间的主要原因是您在 Get-Users 函数中查询所有已启用的用户,然后在 Get-AllAzureADUsers 上再次查询它们 2 次 函数。您还可以一次查询并构建所需的输出。

您还按名称(排序对象名称)进行排序,但您的Get-AllAzureADUsers函数未返回具有该名称的对象属性,因此除了枚举所有输出之外什么都不做。

最后,您使用 ForEach-Object 枚举所有用户,这可能是 PowerShell 中最慢的循环。

关于powershell - 包含员工 ID 和经理的 AzureAD 完整花名册报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72121885/

相关文章:

xml - 确定 XML 节点存在

regex - 使用 Powershell 将第二次出现的 "-"替换为 "_"

azure - 执行 Azure 函数 cmdlet Get-AzADAppCredential 的权限不足

azure - 是否有 Azure powershell cmdlet 可以获取订阅中 ARM VM 的核心、CPU

powershell - 使用 powershell 将 Azure 网站平台从 32 位更改为 64 位

string - 如何在Powershell中替换字符串?

powershell - 为什么从 Windows 批处理文件中运行 PowerShell 脚本时会输出不需要的空行?

.net - 在Powershell中交互使用Mutexes(et al)

azure - 有没有办法以编程方式更新 Azure Function 插槽中的应用程序设置?

azure - 通过脚本为 Azure 流分析作业创建警报