如何用PowerShell构建AD服务器选择菜单并执行远程运维任务
PowerShell AD服务器批量运维GUI工具实现方案
前置要求
- 运行脚本的设备需要加入域,且已安装AD PowerShell模块
- 所有目标服务器需提前开启WinRM服务,域环境可通过组策略批量配置,无需逐个操作
- 运行脚本的账号需拥有所有目标服务器的本地管理员权限
核心实现代码
你现有获取AD服务器列表的代码可直接复用,调整转义符后如下:
Import-Module ActiveDirectory $serverList = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' ` -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,Operatingsystem,OperatingSystemVersion,IPv4Address
以下是完整的带GUI的实现代码,包含服务器下拉选择框、操作下拉选择框和执行按钮,所有操作通过WinRM远程执行,无需登录目标服务器:
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName Microsoft.VisualBasic # 加载AD模块获取服务器列表 Import-Module ActiveDirectory $serverList = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' ` -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address | Sort-Object -Property Operatingsystem | Select-Object -Property Name,IPv4Address # 创建主窗口 $mainForm = New-Object System.Windows.Forms.Form $mainForm.Text = "AD服务器批量运维工具" $mainForm.Size = New-Object System.Drawing.Size(400,300) $mainForm.StartPosition = "CenterScreen" # 服务器选择标签 $serverLabel = New-Object System.Windows.Forms.Label $serverLabel.Location = New-Object System.Drawing.Point(20,20) $serverLabel.Size = New-Object System.Drawing.Size(100,20) $serverLabel.Text = "选择目标服务器:" $mainForm.Controls.Add($serverLabel) # 服务器下拉框 $serverDropdown = New-Object System.Windows.Forms.ComboBox $serverDropdown.Location = New-Object System.Drawing.Point(130,20) $serverDropdown.Size = New-Object System.Drawing.Size(220,20) $serverDropdown.DropDownStyle = "DropDownList" # 填充服务器列表,显示主机名+IP foreach ($server in $serverList) { $serverDropdown.Items.Add("$($server.Name) ($($server.IPv4Address))") | Out-Null } $mainForm.Controls.Add($serverDropdown) # 操作选择标签 $opLabel = New-Object System.Windows.Forms.Label $opLabel.Location = New-Object System.Drawing.Point(20,70) $opLabel.Size = New-Object System.Drawing.Size(100,20) $opLabel.Text = "选择执行操作:" $mainForm.Controls.Add($opLabel) # 操作下拉框 $opDropdown = New-Object System.Windows.Forms.ComboBox $opDropdown.Location = New-Object System.Drawing.Point(130,70) $opDropdown.Size = New-Object System.Drawing.Size(220,20) $opDropdown.DropDownStyle = "DropDownList" # 填充操作选项 $opList = @("服务器重启","删除指定文件","移动指定文件","解锁占用文件") foreach ($op in $opList) { $opDropdown.Items.Add($op) | Out-Null } $mainForm.Controls.Add($opDropdown) # 执行按钮 $execBtn = New-Object System.Windows.Forms.Button $execBtn.Location = New-Object System.Drawing.Point(130,120) $execBtn.Size = New-Object System.Drawing.Size(100,30) $execBtn.Text = "执行操作" $execBtn.Add_Click({ $selectedServer = $serverDropdown.SelectedItem.ToString().Split(" ")[0] $selectedOp = $opDropdown.SelectedItem.ToString() if (-not $selectedServer -or -not $selectedOp) { [System.Windows.Forms.MessageBox]::Show("请先选择服务器和操作","提示",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning) return } # 二次确认避免误操作 $confirm = [System.Windows.Forms.MessageBox]::Show("确认要在服务器 $selectedServer 执行 $selectedOp 操作?","操作确认",[System.Windows.Forms.MessageBoxButtons]::YesNo,[System.Windows.Forms.MessageBoxIcon]::Question) if ($confirm -ne [System.Windows.Forms.DialogResult]::Yes) { return } # 操作逻辑实现 switch ($selectedOp) { "服务器重启" { Invoke-Command -ComputerName $selectedServer -ScriptBlock { Restart-Computer -Force } [System.Windows.Forms.MessageBox]::Show("重启指令已发送至 $selectedServer","操作结果",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) } "删除指定文件" { $filePath = [Microsoft.VisualBasic.Interaction]::InputBox("请输入要删除的文件完整路径:","输入文件路径") if ($filePath) { $res = Invoke-Command -ComputerName $selectedServer -ScriptBlock { param($path) if (Test-Path $path) { Remove-Item -Path $path -Force return "文件删除成功" } else { return "文件不存在" } } -ArgumentList $filePath [System.Windows.Forms.MessageBox]::Show($res,"操作结果",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) } } "移动指定文件" { $sourcePath = [Microsoft.VisualBasic.Interaction]::InputBox("请输入要移动的文件完整源路径:","输入源路径") $targetPath = [Microsoft.VisualBasic.Interaction]::InputBox("请输入目标路径:","输入目标路径") if ($sourcePath -and $targetPath) { $res = Invoke-Command -ComputerName $selectedServer -ScriptBlock { param($source,$target) if (Test-Path $source) { Move-Item -Path $source -Destination $target -Force return "文件移动成功" } else { return "源文件不存在" } } -ArgumentList $sourcePath,$targetPath [System.Windows.Forms.MessageBox]::Show($res,"操作结果",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) } } "解锁占用文件" { $filePath = [Microsoft.VisualBasic.Interaction]::InputBox("请输入要解锁的文件完整路径:","输入文件路径") if ($filePath) { $res = Invoke-Command -ComputerName $selectedServer -ScriptBlock { param($path) $processes = Get-Process | Where-Object { $_.Modules.FileName -contains $path } if ($processes) { $processes | Stop-Process -Force return "已结束占用进程,文件解锁成功" } else { return "文件未被占用" } } -ArgumentList $filePath [System.Windows.Forms.MessageBox]::Show($res,"操作结果",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) } } } }) $mainForm.Controls.Add($execBtn) # 显示窗口 $mainForm.ShowDialog() | Out-Null
内容的提问来源于stack exchange,提问作者Herculeon
相关产品推荐
相关产品推荐

