使用 Office 365 PowerShell 管理用户帐户和许可证(八)使用 Office 365 PowerShell 冻结用户账户
使用 Office 365 PowerShell 冻结用户账户
上一次修改主题:2017-04-14
说明如何使用 Office 365 PowerShell 来锁定、禁用和阻止访问 Office 365 帐户。
阻止访问 Office 365 帐户可防止任何人使用该帐户登录并访问 Office 365 组织中的服务和数据。如果阻止了对帐户的访问,当用户尝试登录时会收到下面的错误消息:
您可以使用 Office 365 PowerShell 阻止对单个和多个用户帐户的访问。
-
本主题中的步骤需要您连接到 Office 365 PowerShell。有关说明,请参阅连接到 Office 365 PowerShell。
-
如果阻止了用户帐户,可能最长需要 24 小时才可在用户的所有设备和客户端上生效。
使用以下语法来阻止对单个用户帐户的访问:
Set-MsolUser -UserPrincipalName <Account> -BlockCredential $true
此示例阻止访问用户帐户 [email protected]。
Set-MsolUser -UserPrincipalName [email protected] -BlockCredential $true
若要取消阻止该用户帐户,请运行以下命令:
Set-MsolUser -UserPrincipalName [email protected] -BlockCredential $false
-
创建一个文本文件,在它的每一行上包含一个帐户,如下所示:
在此示例中,该文本文件是 C:\My Documents\Accounts.txt。
-
若要阻止访问该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | Set-MsolUser -UserPrincipalName $_.UserPrincipalName -BlockCredential $true
若要解除阻止该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | Set-MsolUser -UserPrincipalName $_.UserPrincipalName -BlockCredential $false
若要使用 Azure Active Directory V2 PowerShell 模块中的 New-AzureADUser cmdlet,首先必须连接到自己的订阅。有关说明,请参阅连接到 Azure Active Directory V2 PowerShell 模块。
连接后,使用下列语法阻止单个用户帐户:
Set-AzureADUser -ObjectID <Account> -AccountEnabled $false
此示例阻止访问用户帐户 [email protected]。
Set-AzureADUser -ObjectID [email protected] -AccountEnabled $false
若要取消阻止此用户帐户,请运行以下命令:
Set-AzureADUser -ObjectID [email protected] -AccountEnabled $true
注意: |
---|
Set-AzureAD cmdlet 中的 -ObjectID 参数可接受帐户名(也称为“用户主体名称”)或帐户的对象 ID。 |
若要显示基于用户名的帐户名,请使用下列命令:
$userName="<User name>" Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
本示例显示名为 Caleb Sills 的用户的帐户名。
$userName="Caleb Sills" Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
若要阻止基于用户名的帐户,请使用下列命令:
$userName="<User name>" Set-AzureADUser -ObjectID (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName -AccountEnabled $false
若要阻止对多个用户帐户的访问,请执行以下操作:
-
创建一个文本文件,在它的每一行上包含一个帐户名,如下所示:
在此示例中,该文本文件是 C:\My Documents\Accounts.txt。
-
若要阻止访问该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | Set-AzureADUSer -ObjectID $_.ObjectID -AccountEnabled $true
若要解除阻止该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | Set-AzureADUSer -ObjectID $_.ObjectID -AccountEnabled $false