多用户并发连接配置脚本
多用户并发连接配置脚本

多用户并发连接配置脚本

RDP 多用户并发连接配置脚本

参考

  • https://songxwn.com/Windows2022-RDS-RD/

需要先设置好 Windows Server RDS 部署和配置授权激活服务

Install-WindowsFeature Remote-Desktop-Services,RDS-Web-Access,RDS-RD-Server,RDS-Connection-Broker,RDS-Licensing -IncludeManagementTools

# 安装角色和指定功能

Restart-Computer -Force 

# 安装后重启

# 在Windows 管理工具 > Remote Desktop Services > 远程桌面授权管理器 > 激活服务器(联网) > 协议号码可以填写6565792,4954438,6879321,5296992,许可类型为RDS 每用户CAL,数量为 9999

脚本

# ===== 多用户并发连接配置脚本 =====

Write-Host "开始配置多用户并发连接..." -ForegroundColor Cyan

# 1. 允许同一用户多个会话
Write-Host "`n[1/4] 允许同一用户多个并发会话..." -ForegroundColor Yellow
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' `
    -Name fSingleSessionPerUser -Value 0

# 2. 创建策略路径
Write-Host "[2/4] 配置会话限制策略..." -ForegroundColor Yellow
$policyPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services'
if (-not (Test-Path $policyPath)) {
    New-Item -Path $policyPath -Force | Out-Null
}

# 3. 设置每用户最大3个会话
Set-ItemProperty -Path $policyPath -Name MaxInstanceCount -Value 3 -Type DWord -Force
Set-ItemProperty -Path $policyPath -Name fSingleSessionPerUser -Value 0 -Type DWord -Force

# 4. 重启远程桌面服务
Write-Host "[3/4] 重启远程桌面服务..." -ForegroundColor Yellow
Restart-Service TermService -Force
Start-Sleep -Seconds 2

# 5. 验证配置
Write-Host "[4/4] 验证配置..." -ForegroundColor Yellow
$config = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'
$policyConfig = Get-ItemProperty -Path $policyPath -ErrorAction SilentlyContinue

Write-Host "`n========== 配置结果 ==========" -ForegroundColor Green
Write-Host "fSingleSessionPerUser: $($config.fSingleSessionPerUser) $(if($config.fSingleSessionPerUser -eq 0){'✅'}else{'❌'})" 
Write-Host "每用户最大会话数: $(if($policyConfig.MaxInstanceCount){$policyConfig.MaxInstanceCount}else{'未设置'})"
Write-Host "远程桌面状态: $(if($config.fDenyTSConnections -eq 0){'已启用 ✅'}else{'已禁用 ❌'})"
Write-Host "============================`n" -ForegroundColor Green

Write-Host "✅ 配置完成!" -ForegroundColor Green
Write-Host "`n⚠️  重要提示:" -ForegroundColor Yellow
Write-Host "1. 现在每个用户可以同时登录最多 3 个会话"
Write-Host "2. 不同用户之间不会相互抢占"
Write-Host "3. 确保你的 Windows Server 版本和许可证支持多用户 RDS"
Write-Host "`n测试命令: qwinsta (查看当前会话)" -ForegroundColor Cyan