[玩转系统] 使用 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
}
}
这就是它的样子。
运行 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 操作。
强制注销单用户
注销单个用户。
.\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 中强制注销用户。 Set-SignOut PowerShell 不仅可以注销用户,而且当您必须立即对用户帐户应用安全更改时,它是一个很好的脚本。
您喜欢这篇文章吗?您可能还喜欢配置 Office 365 SMTP 中继。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag