首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UserLastLogon -Export

UserLastLogon -Export
EN

Stack Overflow用户
提问于 2021-03-01 23:25:01
回答 1查看 955关注 0票数 2

嗨,我正试图导出一个基于“最后登录”的AD用户列表

我已经使用基本的powershell编写了脚本,但是如果有人能够找到使用"AzureAD to Powershell"命令的解决方案,我会感兴趣的。

我已经得到了列表,但是我不能将它导出到任何文件类型,因为它是如何通过循环生成的。我正在寻找的最终结果是能够组织数据来查看哪些用户已经不活动了?

代码语言:javascript
复制
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); } 
EN

回答 1

Stack Overflow用户

发布于 2021-03-02 04:49:08

在Azure中,我们可以获得所有用户登录记录浅谈Azure门户使用Azure AD PowerShell。如果您正在寻找一种由PowerShell导出具有用户帐户状态的Azure AD用户最后登录列表的方法(无论是否启用),只需尝试下面的代码:

代码语言:javascript
复制
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 请看这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66431130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档