当前位置: 首页 > news >正文

远程桌面管理工具 - 交互式版本

以下是一个带有交互式菜单界面的远程桌面管理 PowerShell 脚本:

<#
.SYNOPSIS远程桌面管理工具 - 交互式版本
.DESCRIPTION提供图形化菜单界面管理远程桌面连接和配置
.NOTES版本: 2.0作者: YourName创建日期: $(Get-Date -Format "yyyy-MM-dd")
#>Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing# 检查管理员权限
function Test-Administrator {$user = [Security.Principal.WindowsIdentity]::GetCurrent()(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}# 显示消息框
function Show-MessageBox {param([string]$Message,[string]$Title = "远程桌面管理工具",[System.Windows.Forms.MessageBoxIcon]$Icon = "Information")[System.Windows.Forms.MessageBox]::Show($Message, $Title, [System.Windows.Forms.MessageBoxButtons]::OK, $Icon)
}# 启用远程桌面
function Enable-RemoteDesktop {try {# 启用远程桌面$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server"Set-ItemProperty -Path $regPath -Name "fDenyTSConnections" -Value 0 -Force# 配置网络级别身份验证(可选)Set-ItemProperty -Path $regPath -Name "UserAuthentication" -Value 1# 添加防火墙规则Enable-NetFirewallRule -DisplayGroup "远程桌面"Show-MessageBox -Message "已成功启用远程桌面并配置防火墙规则" -Icon Informationreturn $true}catch {Show-MessageBox -Message "启用远程桌面时出错: $_" -Icon Errorreturn $false}
}# 禁用远程桌面
function Disable-RemoteDesktop {try {$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server"Set-ItemProperty -Path $regPath -Name "fDenyTSConnections" -Value 1 -ForceShow-MessageBox -Message "已禁用远程桌面连接" -Icon Informationreturn $true}catch {Show-MessageBox -Message "禁用远程桌面时出错: $_" -Icon Errorreturn $false}
}# 检查远程桌面状态
function Get-RemoteDesktopStatus {try {$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server"$denyConnections = Get-ItemProperty -Path $regPath -Name "fDenyTSConnections" -ErrorAction SilentlyContinue$status = "远程桌面状态:`n"if ($denyConnections.fDenyTSConnections -eq 0) {$status += "✓ 远程桌面已启用`n"} else {$status += "✗ 远程桌面已禁用`n"}# 检查防火墙规则$firewallRule = Get-NetFirewallRule -DisplayGroup "远程桌面" -ErrorAction SilentlyContinue | Where-Object { $_.Enabled -eq "True" }if ($firewallRule) {$status += "✓ 远程桌面防火墙规则已启用"} else {$status += "✗ 远程桌面防火墙规则未启用"}Show-MessageBox -Message $status -Icon Information}catch {Show-MessageBox -Message "检查远程桌面状态时出错: $_" -Icon Error}
}# 连接到远程桌面
function Connect-RemoteDesktop {$form = New-Object System.Windows.Forms.Form$form.Text = "连接到远程桌面"$form.Size = New-Object System.Drawing.Size(350, 250)$form.StartPosition = "CenterScreen"$form.FormBorderStyle = "FixedDialog"$form.MaximizeBox = $false$form.MinimizeBox = $false# 计算机名标签和文本框$labelComputer = New-Object System.Windows.Forms.Label$labelComputer.Location = New-Object System.Drawing.Point(20, 20)$labelComputer.Size = New-Object System.Drawing.Size(100, 20)$labelComputer.Text = "计算机名/IP:"$form.Controls.Add($labelComputer)$textBoxComputer = New-Object System.Windows.Forms.TextBox$textBoxComputer.Location = New-Object System.Drawing.Point(120, 20)$textBoxComputer.Size = New-Object System.Drawing.Size(180, 20)$form.Controls.Add($textBoxComputer)# 用户名标签和文本框$labelUser = New-Object System.Windows.Forms.Label$labelUser.Location = New-Object System.Drawing.Point(20, 60)$labelUser.Size = New-Object System.Drawing.Size(100, 20)$labelUser.Text = "用户名:"$form.Controls.Add($labelUser)$textBoxUser = New-Object System.Windows.Forms.TextBox$textBoxUser.Location = New-Object System.Drawing.Point(120, 60)$textBoxUser.Size = New-Object System.Drawing.Size(180, 20)$form.Controls.Add($textBoxUser)# 密码标签和文本框$labelPassword = New-Object System.Windows.Forms.Label$labelPassword.Location = New-Object System.Drawing.Point(20, 100)$labelPassword.Size = New-Object System.Drawing.Size(100, 20)$labelPassword.Text = "密码:"$form.Controls.Add($labelPassword)$textBoxPassword = New-Object System.Windows.Forms.TextBox$textBoxPassword.Location = New-Object System.Drawing.Point(120, 100)$textBoxPassword.Size = New-Object System.Drawing.Size(180, 20)$textBoxPassword.PasswordChar = '*'$form.Controls.Add($textBoxPassword)# 连接按钮$buttonConnect = New-Object System.Windows.Forms.Button$buttonConnect.Location = New-Object System.Drawing.Point(120, 140)$buttonConnect.Size = New-Object System.Drawing.Size(80, 30)$buttonConnect.Text = "连接"$buttonConnect.Add_Click({if (-not $textBoxComputer.Text) {Show-MessageBox -Message "请输入计算机名或IP地址" -Icon Warningreturn}$cmd = "mstsc /v:$($textBoxComputer.Text)"if ($textBoxUser.Text -and $textBoxPassword.Text) {# 使用cmdkey保存凭据cmdkey /generic:TERMSRV/$($textBoxComputer.Text) /user:$($textBoxUser.Text) /pass:$($textBoxPassword.Text)}$form.Close()Start-Process "mstsc.exe" -ArgumentList "/v:$($textBoxComputer.Text)"})$form.Controls.Add($buttonConnect)# 取消按钮$buttonCancel = New-Object System.Windows.Forms.Button$buttonCancel.Location = New-Object System.Drawing.Point(220, 140)$buttonCancel.Size = New-Object System.Drawing.Size(80, 30)$buttonCancel.Text = "取消"$buttonCancel.Add_Click({ $form.Close() })$form.Controls.Add($buttonCancel)$form.ShowDialog() | Out-Null
}# 列出远程桌面会话
function Get-RemoteDesktopSessions {$form = New-Object System.Windows.Forms.Form$form.Text = "远程桌面会话列表"$form.Size = New-Object System.Drawing.Size(600, 400)$form.StartPosition = "CenterScreen"# 计算机名标签和文本框$labelComputer = New-Object System.Windows.Forms.Label$labelComputer.Location = New-Object System.Drawing.Point(20, 20)$labelComputer.Size = New-Object System.Drawing.Size(100, 20)$labelComputer.Text = "计算机名/IP:"$form.Controls.Add($labelComputer)$textBoxComputer = New-Object System.Windows.Forms.TextBox$textBoxComputer.Location = New-Object System.Drawing.Point(120, 20)$textBoxComputer.Size = New-Object System.Drawing.Size(180, 20)$textBoxComputer.Text = "localhost"$form.Controls.Add($textBoxComputer)# 查询按钮$buttonQuery = New-Object System.Windows.Forms.Button$buttonQuery.Location = New-Object System.Drawing.Point(320, 20)$buttonQuery.Size = New-Object System.Drawing.Size(80, 23)$buttonQuery.Text = "查询"$form.Controls.Add($buttonQuery)# 列表视图$listView = New-Object System.Windows.Forms.ListView$listView.Location = New-Object System.Drawing.Point(20, 60)$listView.Size = New-Object System.Drawing.Size(540, 280)$listView.View = "Details"$listView.FullRowSelect = $true$listView.GridLines = $true$listView.Columns.Add("会话名", 80)$listView.Columns.Add("用户名", 120)$listView.Columns.Add("ID", 50)$listView.Columns.Add("状态", 80)$listView.Columns.Add("类型", 100)$listView.Columns.Add("设备", 100)$form.Controls.Add($listView)# 查询按钮点击事件$buttonQuery.Add_Click({$listView.Items.Clear()$computer = $textBoxComputer.Texttry {$sessions = qwinsta /server:$computer 2>$nullif ($sessions) {$sessions | ForEach-Object {if ($_ -match "^\s*\d+") {$sessionInfo = $_ -split "\s+" | Where-Object { $_ -ne "" }$item = New-Object System.Windows.Forms.ListViewItem($sessionInfo[0])$item.SubItems.Add($sessionInfo[1])$item.SubItems.Add($sessionInfo[2])$item.SubItems.Add($sessionInfo[3])$item.SubItems.Add($sessionInfo[4])$item.SubItems.Add($sessionInfo[5])$listView.Items.Add($item)}}} else {Show-MessageBox -Message "没有找到远程桌面会话" -Icon Information}}catch {Show-MessageBox -Message "获取会话列表时出错: $_" -Icon Error}})# 关闭按钮$buttonClose = New-Object System.Windows.Forms.Button$buttonClose.Location = New-Object System.Drawing.Point(480, 20)$buttonClose.Size = New-Object System.Drawing.Size(80, 23)$buttonClose.Text = "关闭"$buttonClose.Add_Click({ $form.Close() })$form.Controls.Add($buttonClose)$form.ShowDialog() | Out-Null
}# 主菜单
function Show-MainMenu {$form = New-Object System.Windows.Forms.Form$form.Text = "远程桌面管理工具"$form.Size = New-Object System.Drawing.Size(400, 350)$form.StartPosition = "CenterScreen"$form.FormBorderStyle = "FixedDialog"$form.MaximizeBox = $false$form.MinimizeBox = $false# 标题标签$labelTitle = New-Object System.Windows.Forms.Label$labelTitle.Location = New-Object System.Drawing.Point(20, 20)$labelTitle.Size = New-Object System.Drawing.Size(350, 30)$labelTitle.Text = "远程桌面管理工具"$labelTitle.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 14, [System.Drawing.FontStyle]::Bold)$labelTitle.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter$form.Controls.Add($labelTitle)# 连接按钮$buttonConnect = New-Object System.Windows.Forms.Button$buttonConnect.Location = New-Object System.Drawing.Point(100, 70)$buttonConnect.Size = New-Object System.Drawing.Size(200, 40)$buttonConnect.Text = "连接到远程桌面"$buttonConnect.Add_Click({$form.Hide()Connect-RemoteDesktop$form.Show()})$form.Controls.Add($buttonConnect)# 启用RDP按钮$buttonEnableRDP = New-Object System.Windows.Forms.Button$buttonEnableRDP.Location = New-Object System.Drawing.Point(100, 120)$buttonEnableRDP.Size = New-Object System.Drawing.Size(200, 40)$buttonEnableRDP.Text = "启用远程桌面"$buttonEnableRDP.Add_Click({if (Test-Administrator) {$form.Hide()Enable-RemoteDesktop$form.Show()} else {Show-MessageBox -Message "此操作需要管理员权限" -Icon Warning}})$form.Controls.Add($buttonEnableRDP)# 禁用RDP按钮$buttonDisableRDP = New-Object System.Windows.Forms.Button$buttonDisableRDP.Location = New-Object System.Drawing.Point(100, 170)$buttonDisableRDP.Size = New-Object System.Drawing.Size(200, 40)$buttonDisableRDP.Text = "禁用远程桌面"$buttonDisableRDP.Add_Click({if (Test-Administrator) {$form.Hide()Disable-RemoteDesktop$form.Show()} else {Show-MessageBox -Message "此操作需要管理员权限" -Icon Warning}})$form.Controls.Add($buttonDisableRDP)# 状态按钮$buttonStatus = New-Object System.Windows.Forms.Button$buttonStatus.Location = New-Object System.Drawing.Point(100, 220)$buttonStatus.Size = New-Object System.Drawing.Size(200, 40)$buttonStatus.Text = "查看远程桌面状态"$buttonStatus.Add_Click({$form.Hide()Get-RemoteDesktopStatus$form.Show()})$form.Controls.Add($buttonStatus)# 会话列表按钮$buttonSessions = New-Object System.Windows.Forms.Button$buttonSessions.Location = New-Object System.Drawing.Point(100, 270)$buttonSessions.Size = New-Object System.Drawing.Size(200, 40)$buttonSessions.Text = "查看远程桌面会话"$buttonSessions.Add_Click({$form.Hide()Get-RemoteDesktopSessions$form.Show()})$form.Controls.Add($buttonSessions)$form.ShowDialog() | Out-Null
}# 启动主菜单
Show-MainMenu

功能说明
这个交互式版本提供了以下功能:

图形化菜单界面:所有功能通过窗口按钮操作,无需记忆命令

主要功能:

连接到远程桌面(支持用户名和密码)

启用远程桌面(需要管理员权限)

禁用远程桌面(需要管理员权限)

查看远程桌面状态

查看远程桌面会话列表

用户友好特性:

自动检测管理员权限

密码输入框隐藏显示

操作结果通过消息框反馈

会话列表以表格形式展示

使用方法
将脚本保存为 .ps1 文件,例如 RemoteDesktopGUI.ps1

右键选择"使用 PowerShell 运行"或从 PowerShell 控制台执行

根据需要使用各个功能按钮

注意事项
某些功能(如启用/禁用远程桌面)需要管理员权限

首次运行可能需要修改执行策略:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

密码以明文形式传递,生产环境中应考虑更安全的方式

这个交互式版本比命令行版本更直观易用,特别适合不熟悉 PowerShell 命令的用户使用。

http://www.dtcms.com/a/335535.html

相关文章:

  • 达梦数据库常见的重要巡检语句
  • Qt5 的基础知识
  • 【UEFI系列】ACPI
  • 51单片机-驱动独立按键模块教程
  • 类的静态成员的定义、调用及继承详解【C++每日一学】
  • AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年8月17日第163弹
  • 深度学习-计算机视觉-数据增广/图像增广
  • 《MATLAB绘图进阶教程》主要内容与专栏目录(持续更新中。。。)
  • GitHub 热榜项目 - 日榜(2025-08-17)
  • 智能体与MCP的核心流程和差异点(适合初学者)
  • IDEA飞算插件测评:重塑AI编码价值的实战体验
  • 【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
  • GEO(生成引擎优化)是什么?GEO优化怎么做
  • 在QML中使用Chart组件
  • Java Stream ForEach算子实现:ForEachOps
  • 半敏捷卫星观测调度系统的设计与实现
  • Git登录配置的详细方法
  • CSS中linear-gradient 的用法
  • Python字符串净化完全指南:专业级字符清理技术与实战
  • 开发者说 | EmbodiedGen:为具身智能打造可交互3D世界生成引擎
  • 区块链练手项目(持续更新)
  • Linux入门指南:基础开发工具---vim
  • 飞算AI 3.2.0实战评测:10分钟搭建企业级RBAC权限系统
  • ZKmall开源商城的移动商城搭建:Uni-app+Vue3 实现多端购物体验
  • PostgreSQL——用户管理
  • 轻松配置NAT模式让虚拟机上网
  • Go语言企业级权限管理系统设计与实现
  • Bootstrap4 轮播详解
  • Apollo 凭什么能 “干掉” 本地配置?
  • 使用Ansys Fluent进行倒装芯片封装Theta-JA热阻表征