1. 引言:运维效率的痛点与自动化曙光
- 运维日常的重复性劳动:硬件检测、软件信息收集、系统状态巡检。
- 图吧工具箱:一个被低估的"瑞士军刀":介绍其丰富的单机检测功能。
- 从手动到自动的跨越:提出利用脚本批量调用图吧工具,解放双手的核心价值。
- 本文目标:指导运维人员编写脚本,实现自动化批量检测与报告生成。
2. 环境与工具准备
-
图吧工具箱的获取与部署:便携版与安装版的选择,统一部署路径的重要性。
-
脚本语言的选择:为什么选择 PowerShell/Batch/Python?各自优势分析。
下表从多个维度对比三种常用脚本语言,帮助您根据实际环境做出选择:
维度 PowerShell Batch (CMD) Python 远程执行能力 强大,原生支持 WinRM、SSH,可远程执行命令和脚本 极弱,基本不支持远程执行,依赖第三方工具(如 PsExec) 优秀,通过 paramiko、fabric 等库支持 SSH,也可集成 WinRM 错误处理 完善,支持 try-catch-finally、$ErrorActionPreference、$?自动变量简陋,仅 errorlevel判断,无结构化异常处理强大,完整的 try-except-else-finally异常处理机制第三方库依赖 中等,PowerShell Gallery 提供模块,部分功能需额外安装 极少,几乎不依赖外部库,但功能受限 丰富,PyPI 海量库,但部署需安装依赖(可用虚拟环境或打包) 跨平台性 较好,PowerShell Core 支持 Windows、Linux、macOS 极差,仅限 Windows(部分命令在旧版 DOS 兼容) 优秀,原生跨平台,代码通常无需修改即可运行 上手难度 中等,语法类似 C#,面向对象,需熟悉 .NET 框架 简单,命令式语法,学习曲线平缓 中等偏易,语法简洁,但需掌握 Python 基础和环境配置 推荐适用场景 Windows 服务器运维、Active Directory 管理、Exchange 管理、需要深度集成 .NET 的场景 简单的本地批处理、启动器脚本、兼容性要求极高的老旧环境 跨平台自动化、数据处理、Web 服务集成、需要丰富第三方库的复杂任务 -
基础环境配置:确保脚本可执行(如 PowerShell 执行策略)。
-
目标场景定义:机房巡检、新机验收、故障预排查等。
3. 核心思路:如何让脚本"操作"图吧工具箱
- 命令行参数研究:探索图吧工具箱内各组件(如CPU-Z、GPU-Z、DiskInfo)是否支持静默运行、结果导出。
- 进程调用与控制:使用脚本启动、监控并获取工具执行结果。
- 结果捕获与解析:处理工具生成的文本报告、日志文件或屏幕输出。
- 目录与文件管理:为每台被检测机器创建独立的结果文件夹。
4. 实战:编写你的第一个批量检测脚本(以 PowerShell 为例)
4.1 脚本框架搭建
首先定义目标机器列表,支持从文件读取或直接指定:
powershell
# 方式1:直接指定目标机器
$TargetComputers = @("PC-01", "PC-02", "192.168.1.100")
# 方式2:从文件读取(每行一个主机名或IP)
$TargetComputers = Get-Content -Path "C:\Scripts\computers.txt"
# 图吧工具箱安装路径(根据实际安装位置调整)
$ToolboxPath = "C:\Program Files\图吧工具箱"
4.2 核心函数设计
4.2.1 Invoke-ToolboxCheck - 远程或本地执行检测
这个函数负责调用图吧工具箱中的具体工具,以 CPU-Z 为例:
powershell
function Invoke-ToolboxCheck {
param(
[string]$ComputerName = $env:COMPUTERNAME,
[string]$ToolName = "CPU-Z",
[int]$TimeoutSeconds = 30
)
# 构建工具完整路径
$toolExe = switch ($ToolName) {
"CPU-Z" { Join-Path $ToolboxPath "Hardware\CPU\cpuz.exe" }
"GPU-Z" { Join-Path $ToolboxPath "Hardware\GPU\gpuz.exe" }
"CrystalDiskInfo" { Join-Path $ToolboxPath "Hardware\Disk\DiskInfo64.exe" }
"AIDA64" { Join-Path $ToolboxPath "Hardware\System\aida64.exe" }
default { throw "不支持的检测工具: $ToolName" }
}
if (-not (Test-Path $toolExe)) {
Write-Warning "工具 $ToolName 不存在于路径: $toolExe"
return $null
}
try {
# 调用 CPU-Z 的命令行参数示例(静默模式+生成报告)
$arguments = "/txt=`"$env:TEMP\cpuz_report.txt`""
if ($ComputerName -ne $env:COMPUTERNAME) {
# 远程执行(需要 PowerShell Remoting 已启用)
$session = New-PSSession -ComputerName $ComputerName -ErrorAction Stop
$result = Invoke-Command -Session $session -ScriptBlock {
param($exePath, $args)
$process = Start-Process -FilePath $exePath -ArgumentList $args -Wait -NoNewWindow -PassThru
if (Test-Path "$env:TEMP\cpuz_report.txt") {
Get-Content "$env:TEMP\cpuz_report.txt" -Raw
}
} -ArgumentList $toolExe, $arguments -ErrorAction Stop
Remove-PSSession $session
} else {
# 本地执行
$process = Start-Process -FilePath $toolExe -ArgumentList $arguments -Wait -NoNewWindow -PassThru
if (Test-Path "$env:TEMP\cpuz_report.txt") {
$result = Get-Content "$env:TEMP\cpuz_report.txt" -Raw
}
}
# 解析 CPU-Z 报告的关键信息
if ($result) {
$parsedData = @{}
$result -split "`n" | ForEach-Object {
if ($_ -match "Processor\s*:\s*(.+)") {
$parsedData.CPU = $matches[1].Trim()
}
elseif ($_ -match "Clocks\s*\(Core #0\)\s*:\s*(\d+\.\d+) MHz") {
$parsedData.CoreClock = "$($matches[1]) MHz"
}
elseif ($_ -match "Instructions\s*:\s*(.+)") {
$parsedData.Instructions = $matches[1].Trim()
}
}
return [PSCustomObject]@{
ComputerName = $ComputerName
Tool = $ToolName
RawOutput = $result
ParsedData = $parsedData
Timestamp = Get-Date
Success = $true
}
}
return $null
}
catch {
Write-Error "执行 $ToolName 检测失败 ($ComputerName): $_"
return [PSCustomObject]@{
ComputerName = $ComputerName
Tool = $ToolName
Error = $_.Exception.Message
Timestamp = Get-Date
Success = $false
}
}
}
4.2.2 Export-SystemInfo - 整合硬件信息
这个函数整合系统信息和多个工具检测结果:
powershell
function Export-SystemInfo {
param(
[string]$ComputerName = $env:COMPUTERNAME,
[string]$OutputPath = "C:\Reports\SystemInfo_$((Get-Date).ToString('yyyyMMdd_HHmmss')).json"
)
$systemInfo = @{
ComputerName = $ComputerName
Timestamp = Get-Date
Checks = @()
}
# 1. 获取基础系统信息(使用 WMI/CIM)
try {
$os = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $ComputerName -ErrorAction Stop
$computer = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $ComputerName -ErrorAction Stop
$systemInfo.OS = @{
Name = $os.Caption
Version = $os.Version
Architecture = $os.OSArchitecture
LastBootTime = $os.LastBootUpTime
}
$systemInfo.Hardware = @{
Manufacturer = $computer.Manufacturer
Model = $computer.Model
TotalMemoryGB = [math]::Round($computer.TotalPhysicalMemory / 1GB, 2)
NumberOfProcessors = $computer.NumberOfProcessors
}
}
catch {
Write-Warning "获取基础系统信息失败: $_"
}
# 2. 调用多个图吧工具箱工具进行深度检测
$toolsToRun = @("CPU-Z", "GPU-Z", "CrystalDiskInfo")
foreach ($tool in $toolsToRun) {
$checkResult = Invoke-ToolboxCheck -ComputerName $ComputerName -ToolName $tool
if ($checkResult) {
$systemInfo.Checks += $checkResult
}
Start-Sleep -Seconds 2 # 避免工具冲突
}
# 3. 获取磁盘信息(使用 PowerShell 原生命令)
try {
$disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $ComputerName
$systemInfo.Disks = @()
foreach ($disk in $disks) {
$systemInfo.Disks += @{
Drive = $disk.DeviceID
SizeGB = [math]::Round($disk.Size / 1GB, 2)
FreeGB = [math]::Round($disk.FreeSpace / 1GB, 2)
UsedPercent = [math]::Round(($disk.Size - $disk.FreeSpace) / $disk.Size * 100, 2)
}
}
}
catch {
Write-Warning "获取磁盘信息失败: $_"
}
# 4. 获取网络适配器信息
try {
$adapters = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled=True" -ComputerName $ComputerName
$systemInfo.Network = @()
foreach ($adapter in $adapters) {
$systemInfo.Network += @{
Description = $adapter.Description
IPAddress = $adapter.IPAddress -join ", "
MACAddress = $adapter.MACAddress
DHCPEnabled = $adapter.DHCPEnabled
}
}
}
catch {
Write-Warning "获取网络信息失败: $_"
}
# 5. 导出为 JSON 文件
$systemInfo | ConvertTo-Json -Depth 5 | Out-File -FilePath $OutputPath -Encoding UTF8
Write-Host "系统信息已导出到: $OutputPath" -ForegroundColor Green
return [PSCustomObject]$systemInfo
}
4.3 错误处理与超时控制
powershell
function Invoke-SafeCheck {
param(
[string]$ComputerName,
[scriptblock]$ScriptBlock,
[int]$TimeoutSeconds = 60
)
$job = Start-Job -ScriptBlock $ScriptBlock -ArgumentList $ComputerName
try {
$result = Wait-Job -Job $job -Timeout $TimeoutSeconds
if ($result.State -eq "Completed") {
return Receive-Job -Job $job
} else {
Stop-Job -Job $job
Write-Warning "检测 $ComputerName 超时(${TimeoutSeconds}秒)"
return $null
}
}
finally {
Remove-Job -Job $job -Force
}
}
# 使用示例:安全地执行检测
$safeResult = Invoke-SafeCheck -ComputerName "PC-01" -ScriptBlock {
param($computer)
Export-SystemInfo -ComputerName $computer
}
4.4 日志记录
powershell
$LogFile = "C:\Logs\ToolboxCheck_$(Get-Date -Format 'yyyyMMdd').log"
function Write-Log {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "[$timestamp] [$Level] $Message"
Add-Content -Path $LogFile -Value $logEntry -Encoding UTF8
# 同时输出到控制台
switch ($Level) {
"ERROR" { Write-Host $logEntry -ForegroundColor Red }
"WARNING" { Write-Host $logEntry -ForegroundColor Yellow }
"INFO" { Write-Host $logEntry -ForegroundColor Green }
default { Write-Host $logEntry }
}
}
# 在关键位置添加日志记录
Write-Log -Message "开始批量检测任务,目标机器: $($TargetComputers.Count)台" -Level "INFO"
4.5 完整脚本示例
powershell
# 批量检测主脚本
foreach ($computer in $TargetComputers) {
Write-Log -Message "开始检测计算机: $computer" -Level "INFO"
try {
# 执行系统信息收集
$systemInfo = Export-SystemInfo -ComputerName $computer
# 执行特定工具检测
$cpuCheck = Invoke-ToolboxCheck -ComputerName $computer -ToolName "CPU-Z"
$gpuCheck = Invoke-ToolboxCheck -ComputerName $computer -ToolName "GPU-Z"
Write-Log -Message "计算机 $computer 检测完成" -Level "INFO"
}
catch {
Write-Log -Message "计算机 $computer 检测失败: $_" -Level "ERROR"
continue # 继续下一台,不影响整体任务
}
}
Write-Log -Message "批量检测任务完成" -Level "INFO"
5. 进阶:检测报告的自动化生成与汇总
- 数据收集与结构化:将各工具的输出解析为 JSON、CSV 等格式。
- 报告模板:使用 HTML、Markdown 或 Word 模板生成可视化报告。
- 关键指标突出显示:自动标红异常值(如CPU温度过高、硬盘坏道)。
- 报告分发:自动发送邮件、上传至内部Wiki或归档到指定服务器。
6. 集成与扩展:融入现有运维体系
- 与监控系统(如Zabbix, Prometheus)对接:将检测结果作为自定义指标上报。
- 与CMDB(配置管理数据库)联动:自动更新资产硬件信息。
- 定时任务(计划任务/Cron):实现定期自动巡检。
- 在CI/CD流水线中的应用:用于构建节点的环境验证。
7. 避坑指南与最佳实践
- 权限问题:以管理员身份运行脚本的必要性及安全考量。
- 工具版本兼容性:不同版本图吧工具箱的参数差异。
- 网络与远程执行:在域环境或通过SSH/PSSession执行脚本的配置。
- 脚本的可维护性:模块化设计、配置文件分离、添加详细注释。
- 性能考量:并发执行控制,避免同时检测过多机器导致资源争抢。
8. 总结与展望
- 效率提升总结:从"人跑机房"到"脚本跑数据"的转变。
- 技能进阶:通过此项目巩固的脚本编写、工具调用、系统集成能力。
- 开源与分享:鼓励将脚本模板开源,共建运维工具生态。
- 未来展望:结合AI进行异常预测、检测流程的图形化配置界面。