导出 Microsoft 365 用户上次密码更改日期和时间
建议不要强迫用户按计划更改密码,除非有证据表明存在安全漏洞。但是,有时您想检查用户上次更改密码的时间。在本文中,你将了解如何使用 Microsoft Graph PowerShell 导出 Microsoft 365 用户上次密码更改日期和时间。
安装 Microsoft Graph PowerShell
以管理员身份运行 PowerShell 并安装 Microsoft Graph PowerShell 模块。
Install-Module Microsoft.Graph -Force
重要的:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。
推荐阅读:导出 Microsoft 365 管理员角色成员报告
连接到 Microsoft Graph PowerShell
您需要使用正确的权限连接到 Microsoft Graph PowerShell。
Connect-MgGraph -Scopes "User.Read.All"
您是否希望在没有用户交互的情况下进行连接,因为您希望脚本自动运行?使用基于证书的身份验证或客户端密钥进行设置。请阅读连接到 Microsoft Graph PowerShell 一文来了解更多信息。
若要获取所有 Microsoft 365 用户的上次密码更改日期和时间,请运行以下命令。
Get-MgUser -All -Property DisplayName, UserPrincipalName, LastPasswordChangeDateTime | Select-Object -Property DisplayName, UserPrincipalName, LastPasswordChangeDateTime
出现输出。
DisplayName UserPrincipalName LastPasswordChangeDateTime
----------- ----------------- --------------------------
Alison Bell [email protected] 16/04/2024 17:57:46
Amanda Morgan [email protected] 04/06/2024 17:39:05
Audrey Graham [email protected] 11/10/2024 11:12:34
Boris Campbell [email protected] 11/12/2024 17:12:17
虽然输出很棒,但我们希望获得有关每个 Microsoft 365 用户的更多信息。让我们创建一个报告来检索更多属性并将其导出到 CSV 文件。
使用 PowerShell 导出 Microsoft 365 用户上次密码更改报告
Export-M365LastPassChange.ps1 PowerShell 脚本将获取 Microsoft 365/Entra 租户中的所有用户及其上次密码更改日期和时间。之后,它将报告导出为 CSV 文件。您可以使用 Microsoft Excel 或支持 CSV 文件扩展名的任何其他应用程序打开 CSV 文件。
该脚本将收集以下信息:
- ID
- 用户主体名称
- 邮件
- 显示名称
- 密码策略
- 上次密码更改日期时间
- 创建日期时间
- 领域
- 密码最长期限
- 密码年龄
- 过期时间
- 剩余天数
要使用 PowerShell 导出 Microsoft 365 中所有用户上次更改的密码,请按照以下步骤操作:
步骤 1. 下载 Export-M365LastPassChange PowerShell 脚本
上创建两个文件夹(中:)驾驶:
- 脚本
- 温度
下载 Export-M365LastPassChange.ps1 PowerShell 脚本并将其放置在C:脚本文件夹。该脚本会将所有用户及其上次密码更改日期和时间导出到C:温度文件夹。
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字导出-M365LastPassChange.ps1并将其放置在C:脚本文件夹。
<#
.SYNOPSIS
.Export-M365LastPassChange.ps1
.DESCRIPTION
The script exports the last password change date and time, and more properties
for all Microsoft 365 users to a CSV file.
.LINK
www.alitajran.com/export-microsoft-365-users-last-password-change-date-and-time/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
X: x.com/alitajran
.CHANGELOG
V1.00, 12/23/2024 - Initial version
#>
param (
[Parameter(Mandatory = $true)]
[string]$ExportPath
)
# Connect to Microsoft Graph PowerShell
Connect-MgGraph -Scopes "User.Read.All", "Domain.Read.All" -NoWelcome
# Define CSV file export location variable using the parameter provided
$Csvfile = "$ExportPath"
# Retrieve all domain password policies
$domains = Get-MgDomain | Select-Object Id, PasswordValidityPeriodInDays
# Define the properties to retrieve for users
$properties = @(
"Id",
"UserPrincipalName",
"mail",
"displayName",
"PasswordPolicies",
"LastPasswordChangeDateTime",
"CreatedDateTime"
)
# Splat the user parameters
$userParams = @{
Filter = "userType eq 'member' and accountEnabled eq true"
Property = $properties
CountVariable = 'userCount'
ConsistencyLevel = 'Eventual'
All = $true
Verbose = $true
}
# Fetch user information
$users = Get-MgUser @userParams | Select-Object $properties
# Get the current datetime for calculation
$timeNow = Get-Date
# Initialize a List to store user information
$Report = [System.Collections.Generic.List[Object]]::new()
# Process users and gather required data
foreach ($user in $users) {
$userDomain = ($user.userPrincipalName).Split('@')[1]
$maxPasswordAge = ($domains | Where-Object { $_.Id -eq $userDomain }).PasswordValidityPeriodInDays
if ($user.PasswordPolicies -contains "DisablePasswordExpiration" -or $maxPasswordAge -eq 2147483647) {
$ReportLine = [PSCustomObject][Ordered]@{
Id = $user.Id
UserPrincipalName = $user.UserPrincipalName
Mail = $user.mail
DisplayName = $user.DisplayName
PasswordPolicies = $user.PasswordPolicies -join "; "
LastPasswordChangeDateTime = $user.LastPasswordChangeDateTime
CreatedDateTime = $user.CreatedDateTime
Domain = $userDomain
MaxPasswordAge = "Password does not expire"
PasswordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
ExpiresOn = "N/A"
DaysRemaining = "N/A"
}
# Add the report line to the List
$Report.Add($ReportLine)
}
else {
$expiresOn = $user.LastPasswordChangeDateTime.AddDays($maxPasswordAge)
$daysRemaining = (New-TimeSpan -Start $timeNow -End $expiresOn).Days
$ReportLine = [PSCustomObject][Ordered]@{
Id = $user.Id
UserPrincipalName = $user.UserPrincipalName
Mail = $user.mail
DisplayName = $user.DisplayName
PasswordPolicies = $user.PasswordPolicies -join "; "
LastPasswordChangeDateTime = $user.LastPasswordChangeDateTime
CreatedDateTime = $user.CreatedDateTime
Domain = $userDomain
MaxPasswordAge = $maxPasswordAge
PasswordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
ExpiresOn = $expiresOn
DaysRemaining = if ($daysRemaining -le 0) { "Expired" } else { $daysRemaining }
}
# Add the report line to the List
$Report.Add($ReportLine)
}
}
# Sort on UserPrincipalName
$Report = $Report | Sort-Object UserPrincipalName
# Display the results in Out-GridView
$Report | Out-GridView -Title "Microsoft 365 Users Last Password Change Date and Time Report"
# Export results to CSV
$Report | Export-Csv -Path $Csvfile -NoTypeInformation -Encoding utf8
Write-Host "Script completed. Report exported to $Csvfile" -ForegroundColor Cyan
步骤 2. 运行 Export-M365LastPassChange PowerShell 脚本
运行以下命令来运行导出-M365LastPassChange.ps1PowerShell 脚本并创建报告。
C:scripts.Export-M365LastPassChange.ps1 -ExportPath "C:tempLastPassChangeReport.csv"
报告输出将发送到单独窗口 (Out-GridView) 中的交互式表格。
步骤 3. 检查用户上次密码更改日期和时间报告 CSV 文件
Export-M365LastPassChange.ps1 PowerShell 脚本将所有用户及其上次密码更改日期和时间导出到 CSV 文件。
找到文件LastPassChangeReport.csv在路径中C:温度。
使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。
就是这样!
结论
您了解了如何导出 Microsoft 365 用户上次密码更改日期和时间。下次您想要强制用户更改密码并且想要了解状态时,可以运行该脚本并检查日期和时间。最好有一个脚本来创建包含您需要的所有信息的报告。
您喜欢这篇文章吗?您可能还喜欢为本地配置 Microsoft Entra 密码保护。不要忘记关注我们并分享这篇文章。
