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了

下载权限
查看
  • 免费下载
    评论并刷新后下载
    登录后下载
  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余
免责声明:
1.本站所有内容只做学习和交流使用。 版权归原作者所有。
2.保证站内提供的所有可下载源码资源(软件等)都是按“原样”提供,本站未做过任何改动;但本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。
3.本站部分内容均收集于网络!如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。请联系站长邮箱:admin#ibian.online(#换成@)处理!

给TA打赏
共{{data.count}}人
人已打赏
服务器

Linux服务器巡检脚本

2025-3-22 13:54:49

服务器

巡检脚本定时上传FTP

2025-3-22 14:09:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索