如何使用 Powershell 停用 Windows 远程桌面?

发布于 2024-12-17 21:02:18 字数 392 浏览 4 评论 0原文

我们所有的测试箱都在虚拟机(Windows Server 2003/08)上运行,测试人员只能通过远程桌面访问它们。

某些维护步骤需要将所有用户踢出系统并停用通过远程桌面的访问。

我开始在 powershell 中编写维护脚本,并正在寻找一种暂时停用远程桌面的方法。这可能吗,有什么直接的解决方案吗?

到目前为止我已经尝试过:

  • 一位同事建议关闭 netlogon-service,但我可以 仍然使用远程桌面登录。
  • 另一位同事建议禁用端口阻塞
    带有防火墙的远程桌面,但不知何故感觉不太好
    对我来说是对的(?)因为我不想改变系统的一部分 去影响另一部分。我是不是太挑剔了……? ;)

任何提示都高度赞赏。

干杯, 飞鸟

All our testboxes run on VMs (windows server 2003/08) and testers access them via remote desktop only.

Some maintenance steps require to kick all users from the system and deactivate access via remote desktop.

I started to write the maintenance scripts in powershell and am looking for a way to temporarily deactivate remote desktop. Is that possible, any straight-forward solutions to this?

What I have tried so far:

  • A colleague recommended turning-off the netlogon-service, but I can
    still logon with remote-desktop.
  • Another colleague recommended disabling blocking the port for
    remote-desktop with the firewall, but somehow that does not feel
    right to me (?) because I don't want to change one part of a system
    to affect another part. Am I too picky ... ? ;)

Any hints highly appreciated.

Cheers,
Tobi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随梦而飞# 2024-12-24 21:02:18

默认情况下,您需要将

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\fDenyTSConnections 

值设置为 1 以禁用远程桌面,但需要重新启动。

另一种似乎不需要重新启动的方法(未经测试):

$ts=get-WMIObject Win32_TerminalServiceSetting  -computername remotemachinename

$ts.SetAllowTSConnections(0)

You need to set

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\fDenyTSConnections 

value to 1 by default to disable remote desktop but needs to reboot.

Another way that seem not needing reboot (NOT TESTED):

$ts=get-WMIObject Win32_TerminalServiceSetting  -computername remotemachinename

$ts.SetAllowTSConnections(0)
蓝梦月影 2024-12-24 21:02:18

现在我找到了一个非常适合我的解决方案。 Windows Server 2008 附带了一项名为“终端服务服务器耗尽模式"

... TS 服务器耗尽模式可防止新用户登录服务器,同时允许当前登录的用户重新连接到其现有会话。通过等待现有用户保存工作并注销,管理员可以关闭终端服务器进行维护,而不会导致用户数据丢失。

在激活耗尽模式之前,我确保没有人登录,然后使用以下代码激活耗尽模式:

Invoke-Command -ComputerName myServerHostName -ScriptBlock
{
   Set-ItemProperty -Path "HKLM:\SYSTEM\Currentcontrolset\control\Terminal Server" -Name TSServerDrainMode -Value 1
}

虽然我正在更改注册表项,但不需要重新启动服务器即可使更改生效。这无需重新启动即可工作。

当我完成维护工作时,我使用“-Value 0”停用排水模式,并且用户可以再次登录。

效果就像一个魅力!


我原来的答案是:

我通过广泛的网络搜索找到的首选解决方案如下(也未经测试):

$Terminal = Get-WmiObject Win32_Terminal –Computer “ComputerName”
$Terminal.Enable($True)

我发现的其他可能且有趣的代码片段或该主题的变体:

< code>$myWmiObject = Get-WmiObject -namespace “rootCIMV2TerminalServices” -class Win32_Terminal -Computer “ComputerName” -Authentication PacketPrivacy

Set-WmiInstance -namespace “rootCIMV2TerminalServices” -class Win32_Terminal -ComputerName “ComputerName” -Authentication PacketPrivacy -Argument @{fEnableTerminal=0}

Get-WmiObject -ComputerName “ComputerName” -namespace root/cimv2/terminalservices -class Win32_Terminal -身份验证数据包隐私

Now I have found a solution that works perfect for me. Windows Server 2008 comes with a feature called "Terminal Services Server Drain Mode"

... the TS Server Drain Mode prevents new users from logging onto the server, while allowing currently logged on users to reconnect to their existing sessions. By waiting for existing users to save their work and log off, the administrator can take a terminal server down for maintenance without causing user data loss.

Before I activate the drain mode I ensure that no one is logged in and then I active the drain mode with the following code:

Invoke-Command -ComputerName myServerHostName -ScriptBlock
{
   Set-ItemProperty -Path "HKLM:\SYSTEM\Currentcontrolset\control\Terminal Server" -Name TSServerDrainMode -Value 1
}

Although I am changing a registry key, I am not required to reboot the server for the changes to be effective. This works without a reboot.

When I am done performing maintenance work I deactive drain mode with "-Value 0" and users are able to log in again.

Works like a charm!


My original answer was:

My perferred solution that I have found through extensive web search is as follows (also untested):

$Terminal = Get-WmiObject Win32_Terminal –Computer “ComputerName”
$Terminal.Enable($True)

Other possible and interesting code snippets, or variations on the topic, that I have found:

$myWmiObject = Get-WmiObject -namespace “rootCIMV2TerminalServices” -class Win32_Terminal -Computer “ComputerName” -Authentication PacketPrivacy

or

Set-WmiInstance -namespace “rootCIMV2TerminalServices” -class Win32_Terminal -ComputerName “ComputerName” -Authentication PacketPrivacy -Argument @{fEnableTerminal=0}

or

Get-WmiObject -ComputerName “ComputerName” -namespace root/cimv2/terminalservices -class Win32_Terminal -Authentication PacketPrivacy

若沐 2024-12-24 21:02:18

我经常使用这个 gWmi 代码:

#Remote change logon /disable
$TS_Connector = gwmi Win32_TerminalServiceSetting -N "root/cimv2/terminalservices" -computername $ServerName -Authentication PacketPrivacy
$TS_Connector.Logons=1
$TS_Connector.Put()

并用于启用登录

#Remote change logon /enable
$TS_Connector = gwmi Win32_TerminalServiceSetting -N "root/cimv2/terminalservices" -computername $ServerName -Authentication PacketPrivacy
$TS_Connector.Logons=0
$TS_Connector.Put()

而不是 Invoke-command(),因为 nead RCP 打开,并且默认情况下在 Windows 上禁用 RPC 连接

I use this gWmi code frequently :

#Remote change logon /disable
$TS_Connector = gwmi Win32_TerminalServiceSetting -N "root/cimv2/terminalservices" -computername $ServerName -Authentication PacketPrivacy
$TS_Connector.Logons=1
$TS_Connector.Put()

and for enable logons

#Remote change logon /enable
$TS_Connector = gwmi Win32_TerminalServiceSetting -N "root/cimv2/terminalservices" -computername $ServerName -Authentication PacketPrivacy
$TS_Connector.Logons=0
$TS_Connector.Put()

instead of Invoke-command() because nead RCP openned, and RPC connexion are disabled by default on windows

沙与沫 2024-12-24 21:02:18

今天早上在寻找其他东西(巧合)我看到了这个:“使用 PowerShell 检查并启用远程桌面"。

摘要:涉及注册表操作。

Looking for something else this morning (coincidentally) I saw this: "Checking and enabling Remote Desktop with PowerShell".

Summary: involves registry manipulation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文