azure - 微软图形PowerShell : Export CSV List of Users with Managers Name and UPN

标签 azure powershell visual-studio-code microsoft-graph-api

我需要创建一个 PowerShell 脚本,将用户列表从 Azure AD 导出到 CSV,以显示他们的上次登录日期,其中包括用户详细信息,例如姓名、电子邮件和职位以及他们的经理和显示还有姓名和电子邮件。

我已经完成了以下工作,但不确定如何包含他们的上次登录信息以及经理的电子邮件地址。

Connect-MgGraph

$logDate = Get-Date -Format "dd-MMM-yyyy"

$csvFile = "c:\temp\AllAADUsers_$logDate.csv"

Get-MgUser -All -ExpandProperty manager | Select DisplayName, UserPrincipalName, JobTitle, @{Name = 'Manager'; Expression = {$_.Manager.AdditionalProperties.displayName}} | Export-Csv -Path $csvFile 

最佳答案

不幸的是,但截至目前User Graph API不支持在单个查询中检索用户的 managersignInActivity,为此,您需要为每个用户执行 2 个查询:

$getMgUserSplat = @{
    ExpandProperty = 'Manager'
    All            = $true
    Property       = @(
        'DisplayName'
        'UserPrincipalName'
        'JobTitle'
        'Manager'
    )
}

Get-MgUser @getMgUserSplat | ForEach-Object {
    $signIn = Get-MgUser -UserId $_.Id -Select SignInActivity

    [pscustomobject]@{
        DisplayName        = $_.DisplayName
        UserPrincipalName  = $_.UserPrincipalName
        JobTitle           = $_.JobTitle
        ManagerDisplayName = $_.Manager.AdditionalProperties['displayName']
        ManagerEmail       = $_.Manager.AdditionalProperties['mail']
        LastSignInDateTime = $signIn.SignInActivity.LastSignInDateTime
    }
} | Export-Csv path\to\myExport.csv -NoTypeInformation

关于azure - 微软图形PowerShell : Export CSV List of Users with Managers Name and UPN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76630385/

相关文章:

azure - 限制 AzureAD 应用程序对某些用户/组的访问

sql-server - 将 Azure 上的 UTC 转换为 EST 时间(包括夏令时动态版本)

.net - 从 Windows Azure 表下载数据的最佳方式

powershell - Powershell-具有多个属性的组对象PSObject

windows - 按顺序递归重命名所有子文件夹中的文件

regex - 用正则表达式替换时,如何将数字附加到匹配组的末尾?

python - 未检测到 Python 3.8 安装

process - Visual Studio Code,调试子进程不起作用

sql - 应创建什么级别的角色/用户来执行 SQL Azure 中的存储过程?

git - 使用 posh-git 将远程名称添加到 PowerShell 提示符