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

[玩转系统] 强制更改 Microsoft 365 中所有用户的密码

作者:精品下载站 日期:2024-12-14 01:57:52 浏览:14 分类:玩电脑

强制更改 Microsoft 365 中所有用户的密码


您可以在 Microsoft 365 管理中心强制更改用户的密码。但是,如果您想为许多用户执行此操作,最好使用 PowerShell。这是因为它比手动检查用户要快得多。在本文中,你将了解如何使用 PowerShell 强制 Microsoft 365 中的所有用户更改密码。

安装 Microsoft Graph PowerShell

以管理员身份运行 Windows PowerShell 并安装 Microsoft Graph PowerShell。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShellMicrosoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

连接到 Microsoft Graph PowerShell

然后,使用以下范围连接到 Microsoft Graph PowerShell。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All", "Directory.AccessAsUser.All"

强制单个用户更改密码

这将在用户使用密码登录后向用户显示更改密码提示。

$userId = "[email protected]"

$params = @{
	passwordProfile = @{
		forceChangePasswordNextSignIn = $true
	}
}

Update-MgUser -UserId $userId -BodyParameter $params

用户使用密码登录后,会出现更新密码提示。

[玩转系统] 强制更改 Microsoft 365 中所有用户的密码

更改密码并强制更改单个用户的密码

这将更改用户的密码。之后,他们需要使用该密码登录。登录后,他们将收到更新密码的提示。

$UserId = "[email protected]"

$params = @{
    passwordProfile = @{
        forceChangePasswordNextSignIn = $true
        password                      = "B%#X*4BUiHK3%3"
    }
}

Update-MgUser -UserId $userId -BodyParameter $params

强制所有用户更改密码

强制所有用户在下次登录时更改密码。

重要提示:将管理员 UPN 放在第 2 行上,以便将其从脚本中排除。

$allUsers = Get-MgUser -All
$upnToExclude = "[email protected]"

foreach ($user in $allUsers) {
    if ($user.UserPrincipalName -ne $upnToExclude) {
        try {
            $userId = $user.Id
            $params = @{
                passwordProfile = @{
                    forceChangePasswordNextSignIn = $true
                }
            }
            Update-MgUser -UserId $userId -BodyParameter $params
            
            # Log successful update
            Write-Host "Successfully updated password profile for user: $($user.UserPrincipalName)" -ForegroundColor Green
        }
        catch {
            # Log error or failure
            Write-Host "Failed to update password profile for user: $($user.UserPrincipalName). Error: $_" -ForegroundColor Red
        }
    }
    else {
        Write-Host "Skipping user with UPN: $upnToExclude" -ForegroundColor Cyan
    }
}

更改密码并强制所有用户更改密码

强制更改所有用户的密码。使用新密码登录后,他们将收到密码提示以更改密码。

PowerShell 脚本将为每个用户创建一个随机密码,并将其导出到 C:\Temp 中的 CSV 文件 PasswordReset.csv

重要提示:将管理员 UPN 放在第 2 行上,以便将其从脚本中排除。

$allUsers = Get-MgUser -All
$upnToExclude = "[email protected]"
$CSVExportPath = "C:\temp\PasswordReset.csv"
$PasswordLength = 14

# Randomize passwords
function Get-RandomPassword {
    Param(
        [int]$Length
    )
    Begin {
        if ($Length -lt 4) {
            End
        }
        $Numbers = 1..9
        $LettersLower = 'abcdefghijklmnopqrstuvwxyz'.ToCharArray()
        $LettersUpper = 'ABCEDEFHIJKLMNOPQRSTUVWXYZ'.ToCharArray()
        $Special = '!@#$%^&*()=+[{}]/?<>'.ToCharArray()

        # For the 4 character types (upper, lower, numerical, and special)
        $N_Count = [math]::Round($Length * .2)
        $L_Count = [math]::Round($Length * .4)
        $U_Count = [math]::Round($Length * .2)
        $S_Count = [math]::Round($Length * .2)
    }
    Process {
        $Pswrd = $LettersLower | Get-Random -Count $L_Count
        $Pswrd += $Numbers | Get-Random -Count $N_Count
        $Pswrd += $LettersUpper | Get-Random -Count $U_Count
        $Pswrd += $Special | Get-Random -Count $S_Count

        # If the password length isn't long enough (due to rounding), add X special characters
        # Where X is the difference between the desired length and the current length.
        if ($Pswrd.length -lt $Length) {
            $Pswrd += $Special | Get-Random -Count ($Length - $Pswrd.length)
        }

        # Grab the $Pswrd string and randomize the order
        $Pswrd = ($Pswrd | Get-Random -Count $Length) -join ""
    }
    End {
        $Pswrd
    }
}

$Report = [System.Collections.Generic.List[Object]]::new()

foreach ($user in $allUsers) {
    if ($user.UserPrincipalName -ne $upnToExclude) {
        try {
            $userId = $user.Id
            # Generate a random password
            $newPassword = Get-RandomPassword -Length $PasswordLength            
            $params = @{
                passwordProfile = @{
                    forceChangePasswordNextSignIn = $true
                    password                      = $newPassword
                }
            }
            Update-MgUser -UserId $userId -BodyParameter $params
            # Log successful update and add to CSV data
            Write-Host "Successfully updated password for user: $($user.UserPrincipalName) with new password." -ForegroundColor Green
            $ReportLine = [PSCustomObject]@{
                UserPrincipalName = $user.UserPrincipalName
                NewPassword       = $newPassword
            }
            $Report.Add($ReportLine)
        }
        catch {
            Write-Host "Failed to update password for user: $($user.UserPrincipalName). Error: $_" -ForegroundColor Red
            $ReportLine = [PSCustomObject]@{
                UserPrincipalName = $user.UserPrincipalName
                NewPassword       = "Failed to update"
            }
            $Report.Add($ReportLine)
        }
    }
    else {
        Write-Host "Skipping user with UPN: $upnToExclude" -ForegroundColor Cyan
    }
}

# Export data to CSV
$Report | Export-Csv "$CSVExportPath" -NoTypeInformation -Encoding utf8
Write-Host "Passwords have been successfully exported to $CSVExportPath" -ForegroundColor Cyan

就是这样!

了解更多:使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户 »

结论

您了解了如何使用 PowerShell 强制更改 Microsoft 365 中所有用户的密码。使用正确的命令和脚本,您可以立即采取行动。

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

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

取消回复欢迎 发表评论:

关灯