我需要创建一个 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不支持在单个查询中检索用户的 manager
和 signInActivity
,为此,您需要为每个用户执行 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/