gpt4 book ai didi

azure - 用户上次登录 - 导出

转载 作者:行者123 更新时间:2023-12-02 06:59:05 28 4
gpt4 key购买 nike

您好,我正在尝试根据“上次登录”导出 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com