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

[玩转系统] 使用 PowerShell 在 Microsoft 365 中强制注销用户

作者:精品下载站 日期:2024-12-14 18:43:12 浏览:13 分类:玩电脑

使用 PowerShell 在 Microsoft 365 中强制注销用户


我们想要从 Microsoft 365 注销用户。此外,我们想要禁用用户帐户、重置其密码并禁用其注册设备。如果我们想为单个用户、多个用户或所有用户执行此操作该怎么办?在本文中,您将了解如何使用 PowerShell 在 Microsoft 365 中强制注销用户。

设置注销 PowerShell 脚本

它是一个包含多个操作的脚本,可在用户帐户遭到泄露后增强安全性,或帮助您使用 PowerShell 自动执行任务。

Set-SignOut.ps1 PowerShell 脚本适用于 Microsoft 365 并具有以下选项:

  • 重置密码
    “重置密码”操作允许您更改用户帐户的密码,并在下次登录时强制执行密码更改。当您出于安全考虑或用户忘记密码而需要重置用户密码时,这非常有用。通过在下次登录时强制更改密码,您可以确保用户设置新密码以重新获得对其帐户的访问权限。

  • 禁用设备
    “禁用设备”操作针对与用户关联的已注册设备并禁用它们。当设备被禁用时,它会阻止用户访问该特定设备的资源。当您想要限制特定设备(例如丢失或被盗的设备)的访问时,此操作非常有用。

  • 注销
    “注销”操作会将用户从所有活动会话中注销。它会撤销他们的访问令牌并强制他们重新登录才能访问任何资源。此操作在您想要立即终止所有用户会话的情况下非常有用,例如当帐户受到威胁或用户不应再有权访问资源时。

  • 阻止登录
    “阻止登录”操作会禁用用户登录其帐户的能力。通过阻止登录,可以阻止用户访问任何 Azure AD 集成的服务或资源。当您想要暂时或永久限制用户对其帐户和关联资源的访问时,通常会使用此操作。

注意:该脚本可以针对 Microsoft 365 中的单个用户、多个用户或所有用户运行。如果您希望排除用户,可以使用 -Exclude 参数。

安装 Microsoft Graph Powershell

在我们可以继续操作并强制注销用户并更改其密码之前,我们需要安装 Microsoft Graph PowerShell 模块。

重要提示:您需要 Microsoft Graph 2.0 或更高版本才能使用新参数。

注意:我们建议同时安装 Microsoft Graph 和 Microsoft Graph Beta 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。

以管理员身份运行 Windows PowerShell 并检查 Microsoft Graph 版本。

PS C:\> Get-InstalledModule Microsoft.Graph | ft -AutoSize

Version Name            Repository Description                      
------- ----            ---------- -----------                      
2.1.0   Microsoft.Graph PSGallery  Microsoft Graph PowerShell module

检查 Microsoft Graph Beta 版本。

PS C:\> Get-InstalledModule Microsoft.Graph.Beta | ft -AutoSize

Version Name                 Repository Description                      
------- ----                 ---------- -----------                      
2.1.0   Microsoft.Graph.Beta PSGallery  Microsoft Graph PowerShell module

准备 Set-SignOut PowerShell 脚本

(C:)驱动器上创建一个名为Scripts的文件夹。

下载 Set-SignOut.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Set-SignOut.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Set-SignOut.ps1

    .DESCRIPTION
    Reset user(s) password, disable devices, sign-out all sessions, block sign-in.

    .LINK
    www.a-d.site/force-sign-out-users-microsoft-365/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 06/18/2023 - Initial version
    V1.10, 07/24/2023 - Update for Microsoft Graph PowerShell changes
#>

param (
    [switch]$All,
    [switch]$ResetPassword,
    [switch]$DisableDevices,
    [switch]$SignOut,
    [switch]$BlockSignIn,
    [string[]]$Exclude,
    [string[]]$UserPrincipalNames
)

# Check if no switches or parameters are provided
if (-not $All -and -not $ResetPassword -and -not $DisableDevices -and -not $SignOut -and -not $BlockSignIn -and -not $Exclude -and -not $UserPrincipalNames) {
    Write-Host "No switches or parameters provided. Please specify the desired action using switches such as -All, -ResetPassword, -DisableDevices, -SignOut, -BlockSignIn, or provide user principal names using -UserPrincipalNames." -ForegroundColor Yellow
    Exit
}

# Connect to Microsoft Graph API
Connect-MgGraph -Scopes Directory.AccessAsUser.All

# Retrieve all users if -All parameter is specified
if ($All) {
    $Users = Get-MgUser -All
}
else {
    # Filter users based on provided user principal names
    if ($UserPrincipalNames) {
        $Users = $UserPrincipalNames | Foreach-Object { Get-MgUser -Filter "UserPrincipalName eq '$($_)'" }
    }
    else {
        $Users = @()
        Write-Host "No -UserPrincipalNames or -All parameter provided." -ForegroundColor Yellow
    }
}

# Prompt for the new password if -ResetPassword parameter is specified and there are users to process
$NewPassword = ""
if ($ResetPassword -and $Users.Count -gt 0) {
    $NewPassword = Read-Host "Enter the new password"
}

# Check if any excluded users were not found
$ExcludedNotFound = $Exclude | Where-Object { $Users.UserPrincipalName -notcontains $_ }
foreach ($excludedUser in $ExcludedNotFound) {
    Write-Host "Can't find Azure AD account for user $excludedUser" -ForegroundColor Red
}

# Check if any provided users were not found
$UsersNotFound = $UserPrincipalNames | Where-Object { $Users.UserPrincipalName -notcontains $_ }
foreach ($userNotFound in $UsersNotFound) {
    Write-Host "Can't find Azure AD account for user $userNotFound" -ForegroundColor Red
}

foreach ($User in $Users) {
    # Check if the user should be excluded
    if ($Exclude -contains $User.UserPrincipalName) {
        Write-Host "Skipping user $($User.UserPrincipalName)" -ForegroundColor Cyan
        continue
    }
    
    # Flag to indicate if any actions were performed for the user
    $processed = $false  

    # Revoke access if -SignOut parameter is specified
    if ($SignOut) {
        Write-Host "Sign-out completed for account $($User.DisplayName)" -ForegroundColor Green

        # Revoke all signed in sessions and refresh tokens for the account
        $SignOutStatus = Revoke-MgUserSignInSession -UserId $User.Id

        $processed = $true
    }

    # Block sign-in if -BlockSignIn parameter is specified
    if ($BlockSignIn) {
        Write-Host "Block sign-in completed for account $($User.DisplayName)" -ForegroundColor Green

        # Block sign-in
        Update-MgUser -UserId $User.Id -AccountEnabled:$False

        $processed = $true
    }

    # Reset the password if -ResetPassword parameter is specified
    if ($ResetPassword -and $NewPassword) {
        $NewPasswordProfile = @{
            "Password"                      = $NewPassword
            "ForceChangePasswordNextSignIn" = $true
        }
        Update-MgUser -UserId $User.Id -PasswordProfile $NewPasswordProfile
        Write-Host "Password reset completed for $($User.DisplayName)" -ForegroundColor Green

        $processed = $true
    }

    # Disable registered devices if -DisableDevices parameter is specified
    if ($DisableDevices) {
        Write-Host "Disable registered devices completed for $($User.DisplayName)" -ForegroundColor Green
        
        # Retrieve registered devices
        $UserDevices = Get-MgUserRegisteredDevice -UserId $User.Id

        # Disable registered devices
        if ($UserDevices) {
            foreach ($Device in $UserDevices) {
                Update-MgDevice -DeviceId $Device.Id -AccountEnabled $false
            }
        }

        $processed = $true
    }

    if (-not $processed) {
        Write-Host "No actions selected for account $($User.DisplayName)" -ForegroundColor Yellow
    }
}

这就是它的样子。

[玩转系统] 使用 PowerShell 在 Microsoft 365 中强制注销用户

运行 Set-SignOut PowerShell 脚本

转到脚本文件夹并运行脚本。

.\Set-SignOut.ps1

如果不输入任何参数,会提示必须输入参数。

No switches or parameters provided. Please specify the desired action using switches such as -All, -ResetPassword, -DisableDevices, -SignOut, -BlockSignIn, or provide user principal names using -UserPrincipalNames.

添加其中一个开关后,您必须输入您的 Microsoft 管理员凭据并接受请求的权限

[玩转系统] 使用 PowerShell 在 Microsoft 365 中强制注销用户

让我们看看您可以执行哪些 PowerShell 操作。

强制注销单用户

注销单个用户。

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]" -SignOut

阻止单个用户登录。

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]" -BlockSignIn

如果您想阻止单个用户登录并重置其密码。

注意:将出现填写新密码的提示。

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]" -BlockSignIn -ResetPassword

阻止单个用户登录、重置其密码并禁用已注册的设备。

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]" -BlockSignIn -ResetPassword -DisableDevices

强制多用户退出

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]","[email protected]" -SignOut

只需将您喜欢的参数附加到上述命令即可进行设置。

.\Set-SignOut.ps1 -UserPrincipalNames "[email protected]","[email protected]" -ResetPassword -DisableDevices

强制注销所有用户

.\Set-SignOut.ps1 -All -SignOut

只需将您喜欢的参数附加到上述命令即可进行设置。

.\Set-SignOut.ps1 -All -BlockSignIn -ResetPassword

可以添加-Exclude参数来跳过这些用户帐户。

.\Set-SignOut.ps1 -All -BlockSignIn -ResetPassword -Exclude "[email protected]","[email protected]"

该脚本功能强大,因为您只能针对单个用户、多个用户和所有用户(包括您想要排除的用户)运行它。

Outlook 中检测到错误 500 重复重定向

如果您使用 -BlockSignIn 参数,它将阻止用户登录。取消阻止用户登录后,Outlook 网页版在登录后将显示以下错误。

错误 500 出了问题。检测到重复重定向。

解决此错误的方法是等待 30 分钟,然后才能再次运行。

[玩转系统] 使用 PowerShell 在 Microsoft 365 中强制注销用户

就是这样!

了解更多:阻止从共享邮箱登录 »

结论

您了解了如何使用 PowerShell 在 Microsoft 365 中强制注销用户。 Set-SignOut PowerShell 不仅可以注销用户,而且当您必须立即对用户帐户应用安全更改时,它是一个很好的脚本。

您喜欢这篇文章吗?您可能还喜欢配置 Office 365 SMTP 中继。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯