巡检脚本定时上传FTP
基于修改
不在本地留存文件,直接上传到FTP
# 定义输出文件名 $REPORT_FILE = "system_inspection_$(Get-Date -Format 'yyyyMMddHHmm').html" # 生成HTML内容 $htmlContent = @" <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> "@ # 函数:添加报告段落 function Add-Section { param ($Title) $script:htmlContent += "<h2>$Title</h2>" $script:htmlContent += "<table>" } # 函数:结束段落 function End-Section { $script:htmlContent += "</table>" } # 函数:添加表格行 function Add-Row { param ($Item, $Value, $Status) $script:htmlContent += "<tr><th>$Item</th><td>$Value</td><td>$Status</td></tr>" } ############################## # 1. 主机基本信息 ############################## Add-Section "主机基本信息" $htmlContent += @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ $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 "硬件信息" $htmlContent += @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ $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 "磁盘空间检查" $htmlContent += @" <tr><th>磁盘</th><th>使用情况</th><th>状态</th></tr> "@ # 获取所有逻辑磁盘信息 $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 "性能指标" $htmlContent += @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ $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 "安全配置检查" $htmlContent += @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ $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 "网络信息" $htmlContent += @" <tr><th>检查项</th><th>值</th><th>状态</th></tr> "@ $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内容 $htmlContent += @" </body> </html> "@ Write-Output "HTML报告内容已生成,准备上传到FTP服务器。" # 函数:上传文件到FTP服务器 function Upload-ToFTP { param ( [string]$Content, [string]$FileName, [string]$FtpServer, [string]$FtpUser, [string]$FtpPassword ) # 将内容写入临时文件 $tempFile = [System.IO.Path]::GetTempFileName() [System.IO.File]::WriteAllText($tempFile, $Content, [System.Text.Encoding]::UTF8) # 创建FTP请求 $ftpRequest = [System.Net.FtpWebRequest]::Create("$FtpServer/$FileName") $ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile $ftpRequest.Credentials = New-Object System.Net.NetworkCredential($FtpUser, $FtpPassword) $ftpRequest.UseBinary = $true # 读取文件内容并上传 $fileContent = [System.IO.File]::ReadAllBytes($tempFile) $ftpRequestStream = $ftpRequest.GetRequestStream() $ftpRequestStream.Write($fileContent, 0, $fileContent.Length) $ftpRequestStream.Close() # 删除临时文件 Remove-Item -Path $tempFile -Force Write-Output "文件已上传到FTP服务器: $FtpServer/$FileName" } # FTP服务器信息(根据实际修改) $ftpServer = "ftp://100.171.61.158:21" $ftpUser = "systemcheck" $ftpPassword = "jj2JGdxG6fWkE4PmD4Tz" # 上传报告到FTP服务器 Upload-ToFTP -Content $htmlContent -FileName $REPORT_FILE -FtpServer $ftpServer -FtpUser $ftpUser -FtpPassword $ftpPassword
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...