嗨,我正试图导出一个基于“最后登录”的AD用户列表
我已经使用基本的powershell编写了脚本,但是如果有人能够找到使用"AzureAD to Powershell"命令的解决方案,我会感兴趣的。
我已经得到了列表,但是我不能将它导出到任何文件类型,因为它是如何通过循环生成的。我正在寻找的最终结果是能够组织数据来查看哪些用户已经不活动了?
Import-Module ActiveDirectory
function Get-ADUserLastLogon([string]$userName) {
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$time = 0
foreach($dc in $dcs) {
$hostname = $dc.HostName
$user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
if($user.LastLogon -gt $time) {
$time = $user.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
Write-Host $username "last logged on at:" $dt
}
$unames = Get-ADUser -Filter 'ObjectClass -eq "User"' | Select -Expand SamAccountName
foreach ($uname in $unames) { Get-ADUserLastLogon($uname); } 发布于 2021-03-02 04:49:08
在Azure中,我们可以获得所有用户登录记录浅谈Azure门户或使用Azure AD PowerShell。如果您正在寻找一种由PowerShell导出具有用户帐户状态的Azure AD用户最后登录列表的方法(无论是否启用),只需尝试下面的代码:
Connect-AzureAD
$AllUsers = Get-AzureADUser -All $true
$AllSiginLogs = Get-AzureADAuditSignInLogs -All $true
$results = @()
foreach($user in $AllUsers){
$LoginRecord = $AllSiginLogs | Where-Object{ $_.UserId -eq $user.ObjectId } | Sort-Object CreatedDateTime -Descending
if($LoginRecord.Count -gt 0){
$lastLogin = $LoginRecord[0].CreatedDateTime
}else{
$lastLogin = 'no login record'
}
$item = @{
userUPN=$user.UserPrincipalName
userDisplayName = $user.DisplayName
lastLogin = $lastLogin
accountEnabled = $user.AccountEnabled
}
$results += New-Object PSObject -Property $item
}
$results | export-csv -Path d:\result.csv -NoTypeInformation导出到.csv文件结果:

有一件事您应该知道,对于不同的Azure服务层,Azure保存这些数据的时间不同,details 请看这里。
https://stackoverflow.com/questions/66431130
复制相似问题