Windows 服务器巡检脚本
同上一个文章,不过这个是Win的 Linux服务器巡检脚本 - 岸边IBIAN
⚙️系统基本信息: 自动获取操作系统、内核版本、主机名、运行时间等信息,为后续诊断提供基础数据。
🖥️硬件检测: 检测CPU型号、核心数、内存总量、硬盘使用情况等,帮助你全面了解服务器硬件状态。
📈性能监控: 实时监控内存使用率、负载情况、磁盘利用率等关键指标,预防系统过载风险。
🔒配置检查: 自动检查SSH配置、防火墙状态以及其他安全参数,确保系统安全稳固。
🌐网络信息: 收集IP地址、活动网卡等信息,方便进行网络故障排查
C盘检查
# 定义输出文件 $REPORT_FILE = "system_inspection_$(Get-Date -Format 'yyyyMMddHHmm').html" # 开始生成HTML报告 @" <html> <head> <title>Windows系统巡检报告</title> <meta charset="UTF-8"> <style> body { font-family: "Microsoft YaHei", sans-serif; margin: 20px; } h1 { color: #2E8B57; } h2 { color: #2c3e50; border-bottom: 2px solid #2c3e50; padding-bottom: 5px; } /* 统一表格列宽 */ table { border-collapse: collapse; width: 100%; margin-bottom: 20px; table-layout: fixed; } /* 统一单元格格式 */ th, td { border: 1px solid #dddddd; padding: 12px; text-align: center; /* 默认居中对齐 */ vertical-align: middle; /* 上下居中对齐 */ } /* 第一列左对齐 */ th:first-child, td:first-child { text-align: left; width: 20%; /* 让第一列占据相同的宽度 */ } th:nth-child(2), td:nth-child(2) { width: 40%; /* 第二列 */ } th:nth-child(3), td:nth-child(3) { width: 40%; /* 第三列 */ } th { background-color: #f8f9fa; } .warning { background-color: #fff3cd; } .critical { background-color: #f8d7da; } .ok { background-color: #d4edda; } /* 响应式表格 */ @media (max-width: 768px) { table, th, td { font-size: 12px; } } </style> </head> <body> <h1>Windows系统巡检报告</h1> <p>生成时间: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")</p> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 # 函数:添加报告段落 function Add-Section { param ($Title) "<h2>$Title</h2>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append "<table>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } # 函数:结束段落 function End-Section { "</table>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } # 函数:添加表格行 function Add-Row { param ($Item, $Value, $Status) "<tr><th>$Item</th><td>$Value</td><td>$Status</td></tr>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } ############################## # 1. 主机基本信息 ############################## Add-Section "主机基本信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem Add-Row "操作系统" "$($osInfo.Caption) $($osInfo.Version)" "正常" $uptime = (Get-Date) - $osInfo.LastBootUpTime Add-Row "运行时间" "$([math]::Floor($uptime.TotalHours))小时$($uptime.Minutes)分钟" "正常" $hostname = $env:COMPUTERNAME Add-Row "主机名" "$hostname" "正常" End-Section ############################## # 2. 硬件信息 ############################## Add-Section "硬件信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $cpuInfo = Get-CimInstance -ClassName Win32_Processor Add-Row "CPU型号" "$($cpuInfo.Name)" "正常" Add-Row "CPU核心数" "$($cpuInfo.NumberOfCores)" "正常" $memInfo = Get-CimInstance -ClassName Win32_ComputerSystem Add-Row "总内存" "$([math]::Round($memInfo.TotalPhysicalMemory / 1GB, 2)) GB" "正常" $diskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'" Add-Row "C盘使用" "$([math]::Round(($diskInfo.Size - $diskInfo.FreeSpace) / 1GB, 2)) GB / $([math]::Round($diskInfo.Size / 1GB, 2)) GB" "正常" End-Section ############################## # 3. 性能指标 ############################## Add-Section "性能指标" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $cpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue Add-Row "CPU使用率" "$([math]::Round($cpuUsage, 2))%" "正常" $memUsage = (Get-Counter '\Memory\% Committed Bytes In Use').CounterSamples.CookedValue Add-Row "内存使用率" "$([math]::Round($memUsage, 2))%" "正常" $diskUsage = (Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace / (Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'").Size * 100 if ($diskUsage -lt 10) { $status = "<div class='critical'>critical</div>" } elseif ($diskUsage -lt 20) { $status = "<div class='warning'>warning</div>" } else { $status = "<div class='ok'>ok</div>" } Add-Row "C盘剩余空间" "$([math]::Round(100 - $diskUsage, 2))%" "$status" End-Section ############################## # 4. 安全配置检查 ############################## Add-Section "安全配置检查" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $firewallStatus = (Get-NetFirewallProfile).Enabled if ($firewallStatus -contains $true) { $status = "<div class='ok'>ok</div>" } else { $status = "<div class='warning'>warning</div>" } Add-Row "防火墙状态" "$firewallStatus" "$status" End-Section ############################## # 5. 网络信息 ############################## Add-Section "网络信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $ipInfo = (Get-NetIPAddress | Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp' }).IPAddress Add-Row "IP地址" "$ipInfo" "正常" $netStatus = (Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }).Count Add-Row "活动网卡数量" "$netStatus" "正常" End-Section # 结束HTML报告 @" </body> </html> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append Write-Output "巡检报告已生成: $REPORT_FILE"
全盘检查
# 定义输出文件 $REPORT_FILE = "system_inspection_$(Get-Date -Format 'yyyyMMddHHmm').html" # 开始生成HTML报告 @" <html> <head> <title>Windows系统巡检报告</title> <meta charset="UTF-8"> <style> body { font-family: "Microsoft YaHei", sans-serif; margin: 20px; } h1 { color: #2E8B57; } h2 { color: #2c3e50; border-bottom: 2px solid #2c3e50; padding-bottom: 5px; } /* 统一表格列宽 */ table { border-collapse: collapse; width: 100%; margin-bottom: 20px; table-layout: fixed; } /* 统一单元格格式 */ th, td { border: 1px solid #dddddd; padding: 12px; text-align: center; /* 默认居中对齐 */ vertical-align: middle; /* 上下居中对齐 */ } /* 第一列左对齐 */ th:first-child, td:first-child { text-align: left; width: 20%; /* 让第一列占据相同的宽度 */ } th:nth-child(2), td:nth-child(2) { width: 40%; /* 第二列 */ } th:nth-child(3), td:nth-child(3) { width: 40%; /* 第三列 */ } th { background-color: #f8f9fa; } .warning { background-color: #fff3cd; } .critical { background-color: #f8d7da; } .ok { background-color: #d4edda; } /* 响应式表格 */ @media (max-width: 768px) { table, th, td { font-size: 12px; } } </style> </head> <body> <h1>Windows系统巡检报告</h1> <p>生成时间: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")</p> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 # 函数:添加报告段落 function Add-Section { param ($Title) "<h2>$Title</h2>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append "<table>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } # 函数:结束段落 function End-Section { "</table>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } # 函数:添加表格行 function Add-Row { param ($Item, $Value, $Status) "<tr><th>$Item</th><td>$Value</td><td>$Status</td></tr>" | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append } ############################## # 1. 主机基本信息 ############################## Add-Section "主机基本信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem Add-Row "操作系统" "$($osInfo.Caption) $($osInfo.Version)" "正常" $uptime = (Get-Date) - $osInfo.LastBootUpTime Add-Row "运行时间" "$([math]::Floor($uptime.TotalHours))小时$($uptime.Minutes)分钟" "正常" $hostname = $env:COMPUTERNAME Add-Row "主机名" "$hostname" "正常" End-Section ############################## # 2. 硬件信息 ############################## Add-Section "硬件信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $cpuInfo = Get-CimInstance -ClassName Win32_Processor Add-Row "CPU型号" "$($cpuInfo.Name)" "正常" Add-Row "CPU核心数" "$($cpuInfo.NumberOfCores)" "正常" $memInfo = Get-CimInstance -ClassName Win32_ComputerSystem Add-Row "总内存" "$([math]::Round($memInfo.TotalPhysicalMemory / 1GB, 2)) GB" "正常" End-Section ############################## # 3. 磁盘空间检查 ############################## Add-Section "磁盘空间检查" @" <tr><th>磁盘</th><th>使用情况</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append # 获取所有逻辑磁盘信息 $disks = Get-CimInstance -ClassName Win32_LogicalDisk foreach ($disk in $disks) { $driveLetter = $disk.DeviceID $freeSpaceGB = [math]::Round($disk.FreeSpace / 1GB, 2) $totalSpaceGB = [math]::Round($disk.Size / 1GB, 2) $usedSpaceGB = $totalSpaceGB - $freeSpaceGB $usagePercent = [math]::Round(($usedSpaceGB / $totalSpaceGB) * 100, 2) # 根据使用率设置状态 if ($usagePercent -gt 90) { $status = "<div class='critical'>critical</div>" } elseif ($usagePercent -gt 80) { $status = "<div class='warning'>warning</div>" } else { $status = "<div class='ok'>ok</div>" } Add-Row "$driveLetter 盘" "$usedSpaceGB GB / $totalSpaceGB GB ($usagePercent%)" "$status" } End-Section ############################## # 4. 性能指标 ############################## Add-Section "性能指标" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $cpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue Add-Row "CPU使用率" "$([math]::Round($cpuUsage, 2))%" "正常" $memUsage = (Get-Counter '\Memory\% Committed Bytes In Use').CounterSamples.CookedValue Add-Row "内存使用率" "$([math]::Round($memUsage, 2))%" "正常" End-Section ############################## # 5. 安全配置检查 ############################## Add-Section "安全配置检查" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $firewallStatus = (Get-NetFirewallProfile).Enabled if ($firewallStatus -contains $true) { $status = "<div class='ok'>ok</div>" } else { $status = "<div class='warning'>warning</div>" } Add-Row "防火墙状态" "$firewallStatus" "$status" End-Section ############################## # 6. 网络信息 ############################## Add-Section "网络信息" @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append $ipInfo = (Get-NetIPAddress | Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp' }).IPAddress Add-Row "IP地址" "$ipInfo" "正常" $netStatus = (Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }).Count Add-Row "活动网卡数量" "$netStatus" "正常" End-Section # 结束HTML报告 @" </body> </html> "@ | Out-File -FilePath $REPORT_FILE -Encoding UTF8 -Append Write-Output "巡检报告已生成: $REPORT_FILE"
同样放Gitee了
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...