azure - 用户上次登录 - 导出

标签 azure powershell active-directory azure-active-directory

您好,我正在尝试根据“上次登录”导出 AD 用户列表
我已经使用基本 powershell 编写了脚本,但是如果有人可以使用“AzureAD 到 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); } 

最佳答案

在Azure AD中,我们可以获取所有用户登录记录on Azure Portalusing 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 文件 结果: enter image description here

有一点你应该知道,对于不同的 Azure AD 服务层,Azure AD 保留这些数据的时间是不同的,详情 see here .

关于azure - 用户上次登录 - 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66431130/

相关文章:

.net - .NET7 升级后,在本地运行 Azure 函数会出现 "No runtime"错误

azure - 如何在 Cosmos DB 中使用很长的复合索引?

powershell - 术语 '<ScriptPath>\Move' 未被识别为 cmdlet 的名称

powershell - 通过 PowerShell 获取 Sharepoint Online 任务列表中的更改

azure - 如何将 ("Publish") 网站部署到 Azure?

Azure Runbook 重命名 SQL 数据库

powershell - Windows 终端 - 使用不同的配置文件启动 powershell

asp.net-mvc - Asp.net MVC/Silverlight 和 Sharepoint 2010 集成

java - 检测 Activity 目录用户是否必须通过 Java 重置密码

active-directory - : [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, 评论 : AcceptSecurityContext error, 数据 773,v1db1 ]