導出 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 密碼保護。不要忘記關注我們並分享這篇文章。
