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

[玩转系统] 禁用非管理员的远程 PowerShell

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

禁用非管理员的远程 PowerShell


默认情况下,Active Directory 中的每个用户都会启用远程 PowerShell。出于安全原因,强烈建议禁用组织中非管理员和服务帐户的远程 PowerShell 访问。在本文中,您将了解如何禁用非管理员的远程 PowerShell。

在你开始之前

很高兴了解有多种选项可以帮助您禁用非管理员的远程 PowerShell 访问。选择以下最适合您的组织的方法之一。

禁用非管理员的远程 PowerShell

第一种方法是获取所有用户并禁用远程 PowerShell。之后,为需要远程 PowerShell 访问的管理员和服务帐户启用它。

步骤 1. 以管理员身份运行 Exchange 命令行管理程序。

步骤 2. 为所有用户禁用远程 PowerShell。

Get-User -ResultSize Unlimited -Filter 'RemotePowerShellEnabled -eq $true' | Set-User -RemotePowerShellEnabled $false

步骤 3. 验证是否已对所有用户禁用远程 PowerShell。

Get-User -ResultSize Unlimited -Filter 'RemotePowerShellEnabled -eq $true' | ft Name,UserPrincipalName

步骤 4. 为需要的用户启用远程 PowerShell。这很可能是管理员帐户和服务帐户。

Set-User -Identity "userprincipalname" -RemotePowerShellEnabled $true

禁用非管理员的远程 PowerShell(脚本)

第二种方法是每晚或每几个小时将以下 PowerShell 脚本作为计划任务运行。它将确保定义的安全组中的用户帐户启用了远程 PowerShell,并且所有其他用户帐户都禁用了远程 PowerShell。

注意:我们建议使用此方法,因为您创建的新用户将启用远程 PowerShell。安排以下脚本将为不在定义的安全组中的用户自动禁用 RemotePowerShell。

步骤 1. 启动 Active Directory 用户和计算机

步骤 2. 创建一个名为 AllowRemotePS 的安全组。

[玩转系统] 禁用非管理员的远程 PowerShell

第 3 步. 打开 AllowRemotePS 组,单击常规选项卡并填写说明为不在此范围内的用户禁用远程 PowerShell组。

[玩转系统] 禁用非管理员的远程 PowerShell

步骤 4. 单击成员选项卡并添加需要远程 PowerShell 访问的管理员/服务帐户。单击确定

[玩转系统] 禁用非管理员的远程 PowerShell

步骤 5. 下载DisableRemotePS.ps1 PowerShell 脚本并将其放置在C:\scripts\ 文件夹中。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

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

# Load Exchange Management Shell PowerShell Snap-In
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

# Allow Remote PowerShell Group
$AllowRPSGroup = "AllowRemotePS"

# Get all users with enabled Remote PowerShell
$AllUsers = Get-User -ResultSize Unlimited -Filter 'RemotePowerShellEnabled -eq $true' | select SamAccountName, RemotePowerShellEnabled

# Get all users from AllowRPSGroup
$AllowUsers = Get-ADGroupMember $AllowRPSGroup -Recursive | ForEach-Object { Get-User -Identity $_.SamAccountName | select SamAccountName, RemotePowerShellEnabled }

# Enable Remote PowerShell for allowed users
foreach ($AllowUser in $AllowUsers) {
    if ($AllowUser.RemotePowerShellEnabled -eq $false) {
        Set-User $AllowUser.SamAccountName -RemotePowerShellEnabled $true
    }
}

# Disable Remote PowerShell for all users
foreach ($User in $AllUsers) {
    if ($AllowUsers.SamAccountName -notcontains $User.SamAccountName) {
        Set-User $User.SamAccountName -RemotePowerShellEnabled $false
    }
}

步骤 6. 以管理员身份运行 Exchange 命令行管理程序并运行脚本。

C:\scripts\DisableRemotePS.ps1

步骤 7. 验证是否为 AllowRemotePS 安全组用户启用了远程 PowerShell。

Get-User -ResultSize Unlimited -Filter 'RemotePowerShellEnabled -eq $true' | ft Name,UserPrincipalName

第 8 步。 创建计划任务,以便脚本每晚或每几个小时运行一次。

管理员帐户被远程 PowerShell 锁定

如果管理员帐户被锁定,并且您无法连接 Exchange Management Shell,该怎么办?

假设您忘记为管理员帐户启用远程 PowerShell 并且 Exchange Management Shell 不再连接到 Exchange Server,请按照以下步骤操作:

步骤 1. 关闭 Exchange 命令行管理程序

步骤 2. 以管理员身份运行 PowerShell(请注意,这是 PowerShell 而不是 Exchange Management Shell):

步骤 3. 添加 Exchange 管理 PowerShell 管理单元:

Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Snapin

步骤 4. 为管理员帐户启用远程 PowerShell:

Set-User -Identity "userprincipalname" -RemotePowerShellEnabled $true

第 5 步。启动 Exchange Management Shell,一切都会按您的预期运行。

结论

您了解了如何为非管理员和非服务帐户禁用远程 PowerShell。最好的方法是创建一个安全组并添加需要启用远程 PowerShell 的管理员帐户/服务帐户。最后,安排 PowerShell 脚本并专注于其他任务。

您喜欢这篇文章吗?您可能还喜欢保护 Active Directory 密码免遭泄露。不要忘记关注我们并分享这篇文章。

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

取消回复欢迎 发表评论:

关灯