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

增强版计算机管理脚本

针对 compmgmt.msc(计算机管理控制台),我来创建一个增强的PowerShell脚本,提供更灵活的管理功能。

<#
.SYNOPSIS增强版计算机管理脚本
.DESCRIPTION基于compmgmt.msc功能的增强版管理工具,提供更细粒度的控制
.AUTHORAssistant
.DATE2024
#># 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {Write-Warning "需要管理员权限运行此脚本!"Start-Process PowerShell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAsexit
}# 创建日志目录
$LogPath = "C:\Logs\CompMgmtEnhanced"
if (!(Test-Path $LogPath)) {New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
}
$LogFile = "$LogPath\CompMgmt_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"function Write-Log {param([string]$Message, [string]$Type = "INFO")$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"$LogEntry = "[$Timestamp] [$Type] $Message"Write-Host $LogEntry -ForegroundColor $(if ($Type -eq "ERROR") { "Red" } elseif ($Type -eq "WARNING") { "Yellow" } else { "White" })Add-Content -Path $LogFile -Value $LogEntry
}function Open-CompMgmt {Write-Log "打开计算机管理控制台"try {Start-Process compmgmt.mscWrite-Host "计算机管理控制台已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}Read-Host "`n按回车继续"
}function Get-EventViewer {Write-Log "打开事件查看器"try {# 直接打开事件查看器Start-Process "eventvwr.msc"Write-Host "事件查看器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-ServicesManager {Write-Log "打开服务管理器"try {Start-Process "services.msc"Write-Host "服务管理器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-DiskManagement {Write-Log "打开磁盘管理"try {Start-Process "diskmgmt.msc"Write-Host "磁盘管理器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-DeviceManager {Write-Log "打开设备管理器"try {Start-Process "devmgmt.msc"Write-Host "设备管理器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-TaskScheduler {Write-Log "打开任务计划程序"try {Start-Process "taskschd.msc"Write-Host "任务计划程序已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-PerformanceMonitor {Write-Log "打开性能监视器"try {Start-Process "perfmon.msc"Write-Host "性能监视器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-SharedFolders {Write-Log "查看共享文件夹"try {# 使用WMI获取共享文件夹信息$Shares = Get-CimInstance -ClassName Win32_ShareWrite-Host "`n=== 共享文件夹列表 ===" -ForegroundColor Green$Shares | Format-Table Name, Path, Description, @{Name="类型"; Expression={switch ($_.Type) {0 { "磁盘驱动器" }1 { "打印队列" }2 { "设备" }3 { "IPC" }2147483648 { "磁盘驱动器管理" }2147483649 { "打印队列管理" }2147483650 { "设备管理" }2147483651 { "IPC管理" }default { "未知" }}}} -AutoSize# 显示会话和打开文件Write-Host "`n=== 当前会话 ===" -ForegroundColor GreenGet-SmbSession | Format-Table ClientComputerName, UserName, Dialect, NumOpens -AutoSizeWrite-Host "`n=== 打开文件 ===" -ForegroundColor GreenGet-SmbOpenFile | Format-Table Path, ClientComputerName, SessionID, FileId -AutoSize} catch {Write-Host "获取共享信息失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Manage-LocalUsers {Write-Log "管理本地用户和组"try {Start-Process "lusrmgr.msc"Write-Host "本地用户和组管理器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Get-WMIExplorer {Write-Log "打开WMI控制"try {Start-Process "wmimgmt.msc"Write-Host "WMI控制器已启动" -ForegroundColor Green} catch {Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red}
}function Show-SystemSummary {Write-Log "显示系统摘要"# 系统信息$ComputerInfo = Get-CimInstance Win32_ComputerSystem$OSInfo = Get-CimInstance Win32_OperatingSystem$Processor = Get-CimInstance Win32_Processor$Memory = Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property Capacity -SumWrite-Host "`n=== 系统摘要 ===" -ForegroundColor CyanWrite-Host "计算机名: $($env:COMPUTERNAME)" -ForegroundColor YellowWrite-Host "域/工作组: $($ComputerInfo.Domain)" -ForegroundColor YellowWrite-Host "操作系统: $($OSInfo.Caption)" -ForegroundColor YellowWrite-Host "版本: $($OSInfo.Version)" -ForegroundColor YellowWrite-Host "处理器: $($Processor.Name)" -ForegroundColor YellowWrite-Host "内存: $([math]::Round($Memory.Sum/1GB, 2)) GB" -ForegroundColor Yellow# 服务状态统计$Services = Get-Service$RunningServices = ($Services | Where-Object {$_.Status -eq 'Running'}).Count$StoppedServices = ($Services | Where-Object {$_.Status -eq 'Stopped'}).CountWrite-Host "`n=== 服务状态 ===" -ForegroundColor CyanWrite-Host "运行中: $RunningServices" -ForegroundColor GreenWrite-Host "已停止: $StoppedServices" -ForegroundColor Red# 事件日志统计Write-Host "`n=== 最近事件 ===" -ForegroundColor Cyan$SystemErrors = (Get-EventLog -LogName System -EntryType Error -Newest 5).Count$AppErrors = (Get-EventLog -LogName Application -EntryType Error -Newest 5).CountWrite-Host "最近系统错误: $SystemErrors" -ForegroundColor $(if ($SystemErrors -gt 0) { "Red" } else { "Green" })Write-Host "最近应用错误: $AppErrors" -ForegroundColor $(if ($AppErrors -gt 0) { "Red" } else { "Green" })# 磁盘空间Write-Host "`n=== 磁盘空间 ===" -ForegroundColor CyanGet-CimInstance Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | ForEach-Object {$FreePercent = [math]::Round(($_.FreeSpace / $_.Size) * 100, 2)$Color = if ($FreePercent -lt 10) { "Red" } elseif ($FreePercent -lt 20) { "Yellow" } else { "Green" }Write-Host "$($_.DeviceID) 可用空间: $FreePercent%" -ForegroundColor $Color}
}function Export-CompMgmtReport {Write-Log "生成计算机管理报告"$ReportPath = "$LogPath\SystemReport_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"# 收集系统信息$ComputerInfo = Get-CimInstance Win32_ComputerSystem$OSInfo = Get-CimInstance Win32_OperatingSystem$Processor = Get-CimInstance Win32_Processor$Memory = Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum$Disks = Get-CimInstance Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}$Services = Get-Service# 生成HTML报告$HtmlReport = @"
<!DOCTYPE html>
<html>
<head><title>计算机管理报告 - $(Get-Date)</title><style>body { font-family: Arial, sans-serif; margin: 20px; }h1 { color: #2c3e50; }h2 { color: #34495e; border-bottom: 1px solid #bdc3c7; padding-bottom: 5px; }table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }th { background-color: #f2f2f2; }.warning { background-color: #fff3cd; }.error { background-color: #f8d7da; }</style>
</head>
<body><h1>计算机管理报告</h1><p>生成时间: $(Get-Date)</p><p>计算机名: $($env:COMPUTERNAME)</p><h2>系统信息</h2><table><tr><th>项目</th><th>值</th></tr><tr><td>操作系统</td><td>$($OSInfo.Caption)</td></tr><tr><td>版本</td><td>$($OSInfo.Version)</td></tr><tr><td>处理器</td><td>$($Processor.Name)</td></tr><tr><td>总内存</td><td>$([math]::Round($Memory.Sum/1GB, 2)) GB</td></tr><tr><td>制造商</td><td>$($ComputerInfo.Manufacturer)</td></tr><tr><td>型号</td><td>$($ComputerInfo.Model)</td></tr></table><h2>磁盘信息</h2><table><tr><th>驱动器</th><th>总空间(GB)</th><th>可用空间(GB)</th><th>使用率</th></tr>
"@foreach ($Disk in $Disks) {$FreeGB = [math]::Round($Disk.FreeSpace/1GB, 2)$TotalGB = [math]::Round($Disk.Size/1GB, 2)$UsedPercent = [math]::Round(($TotalGB - $FreeGB)/$TotalGB * 100, 2)$RowClass = if ($UsedPercent -gt 90) { "error" } elseif ($UsedPercent -gt 80) { "warning" } else { "" }$HtmlReport += "<tr class='$RowClass'><td>$($Disk.DeviceID)</td><td>$TotalGB</td><td>$FreeGB</td><td>$UsedPercent%</td></tr>"}$HtmlReport += @"</table><h2>服务状态</h2><table><tr><th>状态</th><th>数量</th></tr><tr><td>运行中</td><td>$(($Services | Where-Object {$_.Status -eq 'Running'}).Count)</td></tr><tr><td>已停止</td><td>$(($Services | Where-Object {$_.Status -eq 'Stopped'}).Count)</td></tr></table>
</body>
</html>
"@$HtmlReport | Out-File -FilePath $ReportPath -Encoding UTF8Write-Host "报告已生成: $ReportPath" -ForegroundColor Green# 询问是否打开报告$OpenReport = Read-Host "`n是否打开报告?(y/n)"if ($OpenReport -eq 'y') {Start-Process $ReportPath}
}function Show-Menu {Clear-HostWrite-Host "==========================================" -ForegroundColor CyanWrite-Host "       增强版计算机管理工具" -ForegroundColor CyanWrite-Host "   基于 compmgmt.msc 的增强功能" -ForegroundColor CyanWrite-Host "==========================================" -ForegroundColor CyanWrite-Host "1. 打开计算机管理 (compmgmt.msc)" -ForegroundColor YellowWrite-Host "2. 系统摘要" -ForegroundColor YellowWrite-Host "--- 组件管理 ---" -ForegroundColor WhiteWrite-Host "3. 事件查看器 (eventvwr.msc)" -ForegroundColor GreenWrite-Host "4. 服务管理器 (services.msc)" -ForegroundColor GreenWrite-Host "5. 磁盘管理 (diskmgmt.msc)" -ForegroundColor GreenWrite-Host "6. 设备管理器 (devmgmt.msc)" -ForegroundColor GreenWrite-Host "7. 任务计划程序 (taskschd.msc)" -ForegroundColor GreenWrite-Host "8. 性能监视器 (perfmon.msc)" -ForegroundColor GreenWrite-Host "9. 共享文件夹管理" -ForegroundColor GreenWrite-Host "10. 本地用户和组 (lusrmgr.msc)" -ForegroundColor GreenWrite-Host "11. WMI控制 (wmimgmt.msc)" -ForegroundColor GreenWrite-Host "--- 工具 ---" -ForegroundColor WhiteWrite-Host "12. 生成系统报告" -ForegroundColor MagentaWrite-Host "0. 退出" -ForegroundColor RedWrite-Host "==========================================" -ForegroundColor Cyan
}# 主程序循环
do {try {Show-Menu$Choice = Read-Host "`n请选择功能"switch ($Choice) {'1' { Open-CompMgmt }'2' { Show-SystemSummary; Read-Host "`n按回车返回主菜单" }'3' { Get-EventViewer; Read-Host "`n按回车返回主菜单" }'4' { Get-ServicesManager; Read-Host "`n按回车返回主菜单" }'5' { Get-DiskManagement; Read-Host "`n按回车返回主菜单" }'6' { Get-DeviceManager; Read-Host "`n按回车返回主菜单" }'7' { Get-TaskScheduler; Read-Host "`n按回车返回主菜单" }'8' { Get-PerformanceMonitor; Read-Host "`n按回车返回主菜单" }'9' { Get-SharedFolders; Read-Host "`n按回车返回主菜单" }'10' { Manage-LocalUsers; Read-Host "`n按回车返回主菜单" }'11' { Get-WMIExplorer; Read-Host "`n按回车返回主菜单" }'12' { Export-CompMgmtReport; Read-Host "`n按回车返回主菜单" }'0' { Write-Log "脚本正常退出"Write-Host "感谢使用!" -ForegroundColor Greenexit 0 }default { Write-Host "无效选择,请重新输入!" -ForegroundColor RedStart-Sleep -Seconds 2}}} catch {Write-Host "发生错误: $($_.Exception.Message)" -ForegroundColor RedWrite-Log "脚本错误: $($_.Exception.Message)" "ERROR"Read-Host "按回车继续"}
} while ($true)

脚本特点

🎯 基于 compmgmt.msc 的增强功能:

  1. 直接访问所有compmgmt组件
    • 事件查看器 (eventvwr.msc)
    • 服务管理器 (services.msc)
    • 磁盘管理 (diskmgmt.msc)
    • 设备管理器 (devmgmt.msc)
    • 任务计划程序 (taskschd.msc)
    • 性能监视器 (perfmon.msc)
    • 共享文件夹管理
    • 本地用户和组 (lusrmgr.msc)
    • WMI控制 (wmimgmt.msc)

🔧 额外功能:

  1. 系统摘要 - 快速查看关键系统信息
  2. 共享文件夹管理 - 显示共享、会话和打开文件
  3. HTML报告生成 - 导出详细的系统报告
  4. 日志记录 - 所有操作都有完整日志

💡 使用优势:

  • ✅ 比图形界面更快的访问速度
  • ✅ 可批量执行管理任务
  • ✅ 自动化报告生成
  • ✅ 完整的操作日志
  • ✅ 细粒度的控制选项

🚀 使用方法:

# 直接运行
.\CompMgmtEnhanced.ps1# 或以管理员身份运行
Start-Process PowerShell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `".\CompMgmtEnhanced.ps1`"" -Verb RunAs

这个脚本提供了比原生 compmgmt.msc 更灵活和强大的管理能力,特别适合系统管理员进行日常维护工作。

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

相关文章:

  • 动易网站后台管理系统wordpress homepage plugin
  • 招聘网站对比这么做网站开发实训的心得
  • 河南平台网站建设制作云南网络营销公司
  • 三维重建【0-C】3D Gaussian Splatting:相机标定原理与步骤
  • 好的建站平台简述网站建设的主要内容
  • 公司网站无法收录百度推广是干什么的
  • 可上传多个视频的网站建设沈阳制作网站建站
  • 百度提交网站多久收录怎么搭建个人博客
  • PE之文件结构
  • wordpress创建多站点小型教育网站的开发与建设
  • 如何构建构高性能、高可用、可扩展的集群?
  • 潍坊在线制作网站产品推广外包
  • 海淀教育互动平台seo高效优化
  • Java8:Lambda表达式
  • 医院网站建设 价格石家庄网站优化招聘
  • 【AIGC】2025:MV-Crafter: An Intelligent System for Music-guided Video Generation
  • 电脑版网站建设合同营销网站开发
  • 下载office home and student 2019版本
  • 东莞南城网站建设公司怎么样免费流程图制作网站
  • 锦州做网站公司判断网站模板版本
  • 【测试理论和实践 3.BUG篇】
  • 媒体网站建设包头移动官网网站建设
  • 金华网站建设报价系统优化助手
  • Ninja Ripper游戏模型贴图提取
  • 51c大模型~合集43
  • 做网站开发要学什么网站营销的定义
  • 【GESP/CSP】编程武器库-3, 十六进制转换十进制
  • 客服AI软件正在改变新人培训的方式
  • Vue 3 + Element Plus 表格操作封装:useTableOperations 组合式函数详解
  • 可信网站值得做吗企业网站建设哪家公司好