当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 导出 Microsoft 365 用户密码报告

作者:精品下载站 日期:2024-12-14 05:49:40 浏览:16 分类:玩电脑

导出 Microsoft 365 用户密码报告


您可以在 Microsoft 365 管理中心检查整个组织的密码过期策略。它不显示所有个人用户的列表。若要获取每个 Microsoft 365 用户的详细密码信息,您需要运行 PowerShell 脚本。在本文中,您将了解如何将 Microsoft 365 用户密码报告导出到 CSV 文件。

信息导出 Microsoft 365 用户密码 PowerShell 脚本

如果您的组织尚未设置密码过期策略,Microsoft 建议将密码设置为永不过期。当整个组织都有密码过期策略时,仍然可能有个别 Microsoft 365 用户使用其他策略。这就是 PowerShell 脚本可以发挥作用的地方。

该脚本将运行并收集每个用户的以下信息:

  1. ID
  2. 用户主体名称
  3. 邮件
  4. 显示名称
  5. 密码政策
  6. 上次密码更改日期时间
  7. 创建日期时间
  8. 领域
  9. 密码最长使用期限
  10. 密码年龄
  11. 到期
  12. 剩余天数

我们将在接下来的步骤中向您展示如何获得这些结果。

使用 PowerShell 将 Microsoft 365 用户导出为 CSV

让我们完成这些步骤并使用 PowerShell 将 Microsoft 365 用户密码报告导出到 CSV 文件。

1.安装Microsoft Graph PowerShell

在运行 Export-M365PasswordReport.ps1 PowerShell 脚本之前,您需要安装 Microsoft Graph PowerShell 模块。

运行以下命令来安装 Microsoft Graph 模块。

Install-Module Microsoft.Graph -Force

2. 连接到 Microsoft Graph PowerShell

您需要使用以下范围连接到 MS Graph。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Group.ReadWrite.All", "Directory.ReadWrite.All"
  1. 使用您的管理员凭据登录
  2. 启用代表您的组织同意
  3. 点击接受

[玩转系统] 导出 Microsoft 365 用户密码报告

3.下载Export-M365PasswordReport PowerShell脚本

要下载 Export-M365PasswordReport PowerShell 脚本,请执行以下步骤:

  1. 下载 Export-M365PasswordReport.ps1 PowerShell 脚本
  2. 或者将以下脚本复制到记事本中并将其另存为 Export-M365PasswordReport.ps1 文件
<#
    .SYNOPSIS
    .\Export-M365UsersPassword.ps1

    .DESCRIPTION
    Connect to Microsoft Graph PowerShell first.
    The script exports the passwords report for all Microsoft 365 users to a CSV file.

    .LINK
    Export Microsoft 365 users password report

    .NOTES
    Written By: o365info
    Website:    o365info.com

    .CHANGELOG
    V1.00, 10/26/2023 - Initial version
#>

# Get all domain password expiration policies
$domains = Get-MgDomain | Select-Object Id, PasswordValidityPeriodInDays
$domains | ForEach-Object { if (!$_.PasswordValidityPeriodInDays) { $_.PasswordValidityPeriodInDays = 90 } }

# Get user information 
$properties = "Id", "UserPrincipalName", "mail", "displayName", "PasswordPolicies", "LastPasswordChangeDateTime", "CreatedDateTime"
$users = Get-MgUser -Filter "userType eq 'member' and accountEnabled eq true" `
    -Property $properties -CountVariable userCount `
    -ConsistencyLevel Eventual -All -Verbose | `
    Select-Object $properties | Where-Object {
    "$(($_.userPrincipalName).Split('@')[1])" -in $($domains.id)
}

# Add properties to the $users objects
$users | Add-Member -MemberType NoteProperty -Name Domain -Value $null
$users | Add-Member -MemberType NoteProperty -Name MaxPasswordAge -Value 0
$users | Add-Member -MemberType NoteProperty -Name PasswordAge -Value 0
$users | Add-Member -MemberType NoteProperty -Name ExpiresOn -Value (Get-Date '1970-01-01')
$users | Add-Member -MemberType NoteProperty -Name DaysRemaining -Value 0

# Get the current datetime for calculation
$timeNow = Get-Date

foreach ($user in $users) {
    # Get the user's domain
    $userDomain = ($user.userPrincipalName).Split('@')[1]

    # Check if the user has "disablepasswordexpiration" set
    if ($user.PasswordPolicies -contains "DisablePasswordExpiration") {
        # Set values to indicate that the password does not expire for this user
        $passwordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
        $user.MaxPasswordAge = "Password does not expire"
        $user.PasswordAge = $passwordAge
        $user.ExpiresOn = "N/A"
        $user.DaysRemaining = "N/A"
    }
    else {
        # Get the maximum password age based on the domain password policy
        $maxPasswordAge = ($domains | Where-Object { $_.id -eq $userDomain }).PasswordValidityPeriodInDays

        # Check if MaxPasswordAge is 2147483647
        if ($maxPasswordAge -eq 2147483647) {
            $passwordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
            $user.MaxPasswordAge = "Password does not expire"
            $user.PasswordAge = $passwordAge
            $user.ExpiresOn = "N/A"
            $user.DaysRemaining = "N/A"
        }
        else {
            $passwordAge = (New-TimeSpan -Start $user.LastPasswordChangeDateTime -End $timeNow).Days
            $expiresOn = (Get-Date $user.LastPasswordChangeDateTime).AddDays($maxPasswordAge)
            $user.PasswordAge = $passwordAge
            $user.ExpiresOn = $expiresOn
            $user.DaysRemaining = $(
                # If the remaining days is negative, show "Expired" instead
                if (($daysRemaining = (New-TimeSpan -Start $timeNow -End $expiresOn).Days) -le 0) { "Expired" }
                else { $daysRemaining }
            )
            $user.MaxPasswordAge = $maxPasswordAge
        }
    }

    $user.Domain = $userDomain
}

# Display the results in Out-GridView
$users | Out-GridView

# Export the results to CSV file
$users | Export-Csv -Path "C:\temp\M365UsersPassword.csv" -NoTypeInformation -Encoding UTF8
  1. 如果您还没有 (C:) 驱动器中的 scriptstemp 文件夹,请创建它们
  2. 将 Export-M365PasswordReport.ps1 PowerShell 脚本保存在 C:\scripts 文件夹中

打开文件检查是否已解锁,防止运行脚本时出错。

[玩转系统] 导出 Microsoft 365 用户密码报告

4. 运行 Export-M365PasswordReport PowerShell 脚本

以管理员身份运行 PowerShell 并运行 Export-M365PasswordReport.ps1 PowerShell 脚本。

C:\scripts\.\Export-M365PasswordReport.ps1

PowerShell 脚本开始扫描组织中的所有 Microsoft 365 用户。如果您有很多用户,可能需要几分钟的时间。

5. Out-GridView Microsoft 365 用户密码过期报告

该脚本将在网格视图窗口中显示 Microsoft 365 用户密码信息列表。

Out-GridView 在脚本完成后出现。它显示每个 Microsoft 365 用户拥有的密码策略(无、空、DisableStrongPassword 或 DisablePasswordExpiration)

[玩转系统] 导出 Microsoft 365 用户密码报告

6. 在 Excel 中打开 Microsoft 365 用户密码过期报告 CSV 文件

您将在 C:\temp 文件夹中找到 M365UsersPassword.csv 文件。

[玩转系统] 导出 Microsoft 365 用户密码报告

使用 Microsoft Excel 等应用程序打开 CSV 文件以查看结果。

[玩转系统] 导出 Microsoft 365 用户密码报告

就是这样。

注意:现在您已拥有 Microsoft 365 用户密码报告,您可以管理 Microsoft 365 用户密码。

了解更多:使用 PowerShell 导出 Microsoft 365 邮箱大小报告 »

结论

您了解了如何使用 PowerShell 将 Microsoft 365 用户密码报告导出到 Out-GridView 和 CSV 文件。 Export-M365PasswordReport.ps1 PowerShell 脚本显示每个用户的详细密码报告。您将获得结构化概览,显示每个用户拥有哪些密码策略以及密码何时过期。

您喜欢这篇文章吗?您可能还喜欢导出 Microsoft 365 用户许可证。不要忘记关注我们并分享这篇文章

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯