first commit

This commit is contained in:
2025-06-06 17:14:08 +08:00
commit 7ed04262c3
6 changed files with 1079 additions and 0 deletions

122
README.md Normal file
View File

@@ -0,0 +1,122 @@
# 智能网络监控脚本 - 系统部署版
## 📁 文件结构
```
smart_shutdown/
├── smart_shutdown.ps1 # 原始监控脚本(用户模式)
├── deploy_system.ps1 # 系统级部署脚本
├── uninstall_system.ps1 # 系统级卸载脚本
├── TEST_REPORT.md # 测试报告
├── README.md # 本文件
└── logs/ # 本地测试日志目录
└── network_monitor_20250606.log
```
## 🚀 快速部署
### 系统级部署(推荐)
```powershell
# 以管理员身份运行
.\deploy_system.ps1
```
**部署效果:**
- 程序部署到:`C:\Program Files\SmartNetworkMonitor\`
- 配置和日志:`C:\ProgramData\SmartNetworkMonitor\`
- 开机自启动,无需用户登录
- 以 SYSTEM 权限运行
### 用户级运行
```powershell
# 以管理员身份运行(需要用户登录)
.\smart_shutdown.ps1
```
## 🎯 系统部署后的文件位置
### 程序文件位置
```
C:\Program Files\SmartNetworkMonitor\
├── smart_shutdown_system.ps1 # 系统优化版监控脚本
└── manage.ps1 # 管理工具脚本
```
### 数据文件位置
```
C:\ProgramData\SmartNetworkMonitor\
├── config.json # 配置文件
└── logs\ # 系统日志目录
└── network_monitor_YYYYMMDD.log
```
## 🔧 管理命令
### 查看任务状态
```powershell
Get-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
```
### 启动/停止监控
```powershell
Start-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
Stop-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
```
### 查看日志
```powershell
# 查看今天的日志
Get-Content "C:\ProgramData\SmartNetworkMonitor\logs\network_monitor_$(Get-Date -Format 'yyyyMMdd').log"
# 实时监控日志
Get-Content "C:\ProgramData\SmartNetworkMonitor\logs\network_monitor_$(Get-Date -Format 'yyyyMMdd').log" -Wait
```
### 查看系统事件日志
```powershell
Get-EventLog -LogName Application -Source SmartNetworkMonitor -Newest 20
```
## ⚙️ 配置说明
系统部署后,配置文件位于:`C:\ProgramData\SmartNetworkMonitor\config.json`
默认配置:
```json
{
"TargetIP": "192.168.3.3",
"MonitorWindowSeconds": 180,
"ShutdownCountdown": 60,
"NormalPingInterval": 15
}
```
修改配置文件后,重启监控任务使配置生效:
```powershell
Stop-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
Start-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
```
## 🗑️ 卸载
```powershell
# 以管理员身份运行
.\uninstall_system.ps1
```
## 📊 特性
- ✅ 开机自启动,无需用户登录
- ✅ 系统级权限运行
- ✅ 智能日志管理
- ✅ 配置文件支持
- ✅ 事件日志备份
- ✅ 自动日志清理30天
- ✅ 网络恢复自动取消关机
- ✅ 完整的错误处理
## 🔐 安全说明
- 脚本以 SYSTEM 权限运行,具有执行关机的完整权限
- 所有操作都会记录在日志和系统事件日志中
- 支持通过任务计划程序进行管理和监控

113
TEST_REPORT.md Normal file
View File

@@ -0,0 +1,113 @@
# 智能网络监控脚本测试报告
## 测试日期
2025年6月6日
## 脚本概述
这是一个PowerShell智能网络监控脚本用于监控指定IP的网络连通性当网络持续中断超过设定时间后自动关闭计算机。
## 主要功能
1. **网络连通性监控** - 定期ping目标IP地址
2. **智能故障检测** - 区分临时网络波动和持续故障
3. **可配置监控窗口** - 防止因短暂网络中断导致误关机
4. **关机倒计时机制** - 提供最后的取消机会
5. **完整日志记录** - 记录所有操作和状态变化
6. **手动取消功能** - 用户可以随时按键取消关机
## 配置参数(原版)
- **目标IP**: 192.168.3.3
- **正常监控间隔**: 15秒
- **监控窗口**: 180秒3分钟
- **关机倒计时**: 60秒
- **倒计时ping间隔**: 3秒
- **Ping超时**: 3秒
## 测试配置(测试版)
- **目标IP**: 192.168.3.99不存在的IP模拟网络故障
- **正常监控间隔**: 3秒
- **监控窗口**: 15秒
- **关机倒计时**: 10秒
- **倒计时ping间隔**: 2秒
## 修复的问题
1. **兼容性问题**: 修复了`Test-Connection`命令在旧版PowerShell中不支持`-TimeoutSeconds`参数的问题
2. **编码问题**: 将特殊Unicode字符✓ ✗ 替换为ASCII字符避免控制台显示乱码
## 测试结果
### ✅ 功能测试全部通过
#### 1. 脚本启动功能
- ✅ 正常启动和初始化
- ✅ 日志系统正常工作
- ✅ 配置参数正确加载
- ✅ 本地IP获取功能正常
#### 2. 网络监控功能
- ✅ 正常IP连通性检测如8.8.8.8
- ✅ 故障IP检测如192.168.3.99
- ✅ 网络异常处理机制
- ✅ Ping命令兼容性修复
#### 3. 监控窗口机制
- ✅ 首次网络中断检测
- ✅ 监控窗口计时功能
- ✅ 剩余时间计算和显示
- ✅ 监控窗口超时触发
#### 4. 关机倒计时功能
- ✅ 倒计时启动机制
- ✅ 倒计时期间网络快速检测
- ✅ 按键手动取消功能
- ✅ 模拟关机操作
#### 5. 日志记录功能
- ✅ 日志目录自动创建
- ✅ 按日期分类的日志文件
- ✅ 完整的操作记录
- ✅ 不同级别的日志分类INFO、WARN、CRITICAL、FAIL、SUCCESS
#### 6. 用户界面和显示
- ✅ 彩色控制台输出
- ✅ 清晰的状态提示
- ✅ 编码问题修复
- ✅ 用户友好的信息显示
## 实际测试执行记录
### 测试113:02:43开始
- 监控窗口15秒
- 实际触发时间18秒后进入关机倒计时
- 关机倒计时10秒
- 结果:正常完成模拟关机,重置状态继续监控
### 测试213:08:30开始
- 同样配置重复测试
- 确认功能稳定性和一致性
- 编码问题已完全修复
## 实际使用建议
### 安全配置
1. **取消注释关机命令**:在`smart_shutdown.ps1`第217行`# Stop-Computer -Force`的注释去掉
2. **以管理员身份运行**:脚本需要管理员权限执行关机命令
3. **测试配置**:建议先用较短时间参数测试,确认无误后再使用正式配置
### 最佳实践
1. **监控窗口设置**建议设置为3-5分钟避免网络短暂波动造成误关机
2. **目标IP选择**建议使用网关IP或重要服务器IP
3. **日志监控**:定期检查日志文件,了解网络状况
4. **备用方案**考虑配置多个目标IP进行冗余检测
## 结论
**脚本功能完整,测试全部通过,可以安全投入使用。**
经过全面测试,智能网络监控脚本各项功能均正常工作:
- 网络监控准确可靠
- 故障检测机制有效
- 用户交互友好
- 日志记录完整
- 编码问题已解决
- 兼容性问题已修复
脚本已准备好在生产环境中使用,能够有效监控网络连接并在必要时自动关闭计算机。

519
deploy_system.ps1 Normal file
View File

@@ -0,0 +1,519 @@
<#
.SYNOPSIS
智能网络监控脚本 - 系统级部署脚本
.DESCRIPTION
将脚本部署到系统级目录,配置为开机自启动,无需用户登录
部署位置:
- 程序文件: C:\Program Files\SmartNetworkMonitor\
- 配置文件: C:\ProgramData\SmartNetworkMonitor\
- 日志文件: C:\ProgramData\SmartNetworkMonitor\logs\
#>
# 定义系统级路径
$ProgramPath = "C:\Program Files\SmartNetworkMonitor"
$DataPath = "C:\ProgramData\SmartNetworkMonitor"
$LogPath = "$DataPath\logs"
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "智能网络监控脚本 - 系统级部署" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "部署计划:" -ForegroundColor Yellow
Write-Host "- 程序目录: $ProgramPath" -ForegroundColor White
Write-Host "- 数据目录: $DataPath" -ForegroundColor White
Write-Host "- 日志目录: $LogPath" -ForegroundColor White
Write-Host ""
# 检查源文件
$SourcePath = $PSScriptRoot
$RequiredFiles = @(
"smart_shutdown.ps1"
)
foreach ($file in $RequiredFiles) {
if (-not (Test-Path -Path (Join-Path $SourcePath $file))) {
Write-Error "找不到必需文件: $file"
exit 1
}
}
Write-Host "[OK] 源文件检查完成" -ForegroundColor Green
# 创建目录结构
Write-Host "正在创建目录结构..." -ForegroundColor Yellow
try {
# 创建程序目录
if (-not (Test-Path -Path $ProgramPath)) {
New-Item -Path $ProgramPath -ItemType Directory -Force | Out-Null
Write-Host "[OK] 创建程序目录: $ProgramPath" -ForegroundColor Green
}
# 创建数据目录
if (-not (Test-Path -Path $DataPath)) {
New-Item -Path $DataPath -ItemType Directory -Force | Out-Null
Write-Host "[OK] 创建数据目录: $DataPath" -ForegroundColor Green
}
# 创建日志目录
if (-not (Test-Path -Path $LogPath)) {
New-Item -Path $LogPath -ItemType Directory -Force | Out-Null
Write-Host "[OK] 创建日志目录: $LogPath" -ForegroundColor Green
}
}
catch {
Write-Error "创建目录失败: $($_.Exception.Message)"
exit 1
}
# 创建系统优化版的主脚本
Write-Host "正在创建系统版本的脚本..." -ForegroundColor Yellow
$SystemScriptContent = @"
#Requires -Version 5.1
<#
.SYNOPSIS
-
.DESCRIPTION
-
-
-
#>
# ==================== ====================
# IP
[string]`$TargetIP = "192.168.3.3"
# ping
[int]`$NormalPingInterval = 15
# -
[int]`$MonitorWindowSeconds = 180
#
[int]`$ShutdownCountdown = 60
# ping
[int]`$CountdownPingInterval = 3
#
[string]`$LogDirectory = "$LogPath"
[string]`$LogFile = Join-Path -Path `$LogDirectory -ChildPath "network_monitor_`$(Get-Date -Format 'yyyyMMdd').log"
[string]`$ConfigFile = "$DataPath\config.json"
[int]`$MaxLogDays = 30
#
[int]`$normalLogInterval = 24 # 24 (6)
[int]`$normalLogCounter = 0
# =================================================
# --- ---
#
function Load-Configuration {
if (Test-Path -Path `$ConfigFile) {
try {
`$config = Get-Content -Path `$ConfigFile | ConvertFrom-Json
if (`$config.TargetIP) { `$script:TargetIP = `$config.TargetIP }
if (`$config.MonitorWindowSeconds) { `$script:MonitorWindowSeconds = `$config.MonitorWindowSeconds }
if (`$config.ShutdownCountdown) { `$script:ShutdownCountdown = `$config.ShutdownCountdown }
if (`$config.NormalPingInterval) { `$script:NormalPingInterval = `$config.NormalPingInterval }
Write-Log -Level "INFO" -Message ""
}
catch {
Write-Log -Level "WARN" -Message "使"
}
} else {
#
`$defaultConfig = @{
TargetIP = `$TargetIP
MonitorWindowSeconds = `$MonitorWindowSeconds
ShutdownCountdown = `$ShutdownCountdown
NormalPingInterval = `$NormalPingInterval
}
try {
`$defaultConfig | ConvertTo-Json | Set-Content -Path `$ConfigFile
Write-Log -Level "INFO" -Message ": `$ConfigFile"
}
catch {
Write-Log -Level "WARN" -Message "使"
}
}
}
#
function Write-Log {
param(
[Parameter(Mandatory=`$true)]
[ValidateSet("INFO", "SUCCESS", "FAIL", "WARN", "CRITICAL", "DEBUG")]
[string]`$Level,
[Parameter(Mandatory=`$true)]
[string]`$Message
)
`$logTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
`$logEntry = "[`$logTimestamp] [`$Level] `$Message"
#
try {
if (-not (Test-Path -Path `$LogDirectory)) {
New-Item -Path `$LogDirectory -ItemType Directory -ErrorAction Stop | Out-Null
}
Add-Content -Path `$LogFile -Value `$logEntry -ErrorAction Stop
}
catch {
#
try {
if (-not [System.Diagnostics.EventLog]::SourceExists("SmartNetworkMonitor")) {
New-EventLog -LogName "Application" -Source "SmartNetworkMonitor"
}
Write-EventLog -LogName "Application" -Source "SmartNetworkMonitor" -EventId 1001 -EntryType Information -Message `$logEntry
}
catch {
#
Write-Host "[`$logTimestamp] [CRITICAL] `$Message"
}
}
#
try {
if (`$Level -eq "CRITICAL" -or `$Level -eq "FAIL") {
Write-EventLog -LogName "Application" -Source "SmartNetworkMonitor" -EventId 1002 -EntryType Warning -Message `$logEntry -ErrorAction SilentlyContinue
}
}
catch {
#
}
}
#
function Remove-OldLogs {
try {
`$cutoffDate = (Get-Date).AddDays(-`$MaxLogDays)
`$oldLogs = Get-ChildItem -Path `$LogDirectory -Filter "network_monitor_*.log" | Where-Object { `$_.LastWriteTime -lt `$cutoffDate }
foreach (`$oldLog in `$oldLogs) {
Remove-Item -Path `$oldLog.FullName -Force
Write-Log -Level "INFO" -Message ": `$(`$oldLog.Name)"
}
}
catch {
Write-Log -Level "WARN" -Message ": `$(`$_.Exception.Message)"
}
}
# =================================================
# --- ---
#
Load-Configuration
Remove-OldLogs
#
try {
`$runningAs = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
`$localIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { `$_.InterfaceAlias -notmatch "Loopback" } | Select-Object -First 1).IPAddress
}
catch {
`$runningAs = "Unknown"
`$localIP = "Unknown"
}
Write-Log -Level "INFO" -Message "========== =========="
Write-Log -Level "INFO" -Message ": `$runningAs"
Write-Log -Level "INFO" -Message "IP: `$localIP"
Write-Log -Level "INFO" -Message ": `$TargetIP"
Write-Log -Level "INFO" -Message ": `${MonitorWindowSeconds}"
Write-Log -Level "INFO" -Message ": `${ShutdownCountdown}"
Write-Log -Level "INFO" -Message ": `$LogDirectory"
Write-Log -Level "INFO" -Message ": `$ConfigFile"
#
`$failureStartTime = `$null
while (`$true) {
#
try {
`$pingResult = Test-Connection -ComputerName `$TargetIP -Count 1 -Quiet -ErrorAction SilentlyContinue
`$isOnline = `$pingResult
}
catch {
Write-Log -Level "FAIL" -Message ": `$(`$_.Exception.Message)"
`$isOnline = `$false
}
if (`$isOnline) {
if (`$failureStartTime) {
Write-Log -Level "SUCCESS" -Message ""
`$failureStartTime = `$null
} else {
#
`$normalLogCounter++
if (`$normalLogCounter -ge `$normalLogInterval) {
Write-Log -Level "INFO" -Message " ()"
`$normalLogCounter = 0
}
}
Start-Sleep -Seconds `$NormalPingInterval
} else {
#
Write-Log -Level "FAIL" -Message "Ping - : `$TargetIP"
if (-not `$failureStartTime) {
`$failureStartTime = Get-Date
Write-Log -Level "WARN" -Message ""
}
`$failureDuration = (New-TimeSpan -Start `$failureStartTime -End (Get-Date)).TotalSeconds
Write-Log -Level "INFO" -Message " `$([math]::Round(`$failureDuration)) / `${MonitorWindowSeconds} "
if (`$failureDuration -ge `$MonitorWindowSeconds) {
Write-Log -Level "CRITICAL" -Message " `${MonitorWindowSeconds} "
`$shutdownCancelled = `$false
`$countdownEndTime = (Get-Date).AddSeconds(`$ShutdownCountdown)
while ((Get-Date) -lt `$countdownEndTime -and -not `$shutdownCancelled) {
`$remainingSeconds = [math]::Ceiling((`$countdownEndTime - (Get-Date)).TotalSeconds)
if (`$remainingSeconds -le 0) { break }
Write-Log -Level "WARN" -Message " `$remainingSeconds ... "
#
try {
if (Test-Connection -ComputerName `$TargetIP -Count 1 -Quiet -ErrorAction SilentlyContinue) {
Write-Log -Level "SUCCESS" -Message ""
`$failureStartTime = `$null
`$shutdownCancelled = `$true
break
}
}
catch {
Write-Log -Level "WARN" -Message ": `$(`$_.Exception.Message)"
}
Start-Sleep -Seconds `$CountdownPingInterval
}
if (-not `$shutdownCancelled) {
Write-Log -Level "CRITICAL" -Message ""
try {
#
Stop-Computer -Force
Write-Log -Level "CRITICAL" -Message ""
}
catch {
Write-Log -Level "CRITICAL" -Message ": `$(`$_.Exception.Message)"
}
exit
}
} else {
Start-Sleep -Seconds `$NormalPingInterval
}
}
}
"@
# 写入系统版本脚本
$SystemScriptPath = Join-Path $ProgramPath "smart_shutdown_system.ps1"
try {
$SystemScriptContent | Set-Content -Path $SystemScriptPath -Encoding UTF8
Write-Host "[OK] 创建系统脚本: $SystemScriptPath" -ForegroundColor Green
}
catch {
Write-Error "创建系统脚本失败: $($_.Exception.Message)"
exit 1
}
# 创建默认配置文件
Write-Host "正在创建配置文件..." -ForegroundColor Yellow
$DefaultConfig = @{
TargetIP = "192.168.3.3"
MonitorWindowSeconds = 180
ShutdownCountdown = 60
NormalPingInterval = 15
}
$ConfigPath = Join-Path $DataPath "config.json"
try {
$DefaultConfig | ConvertTo-Json -Depth 10 | Set-Content -Path $ConfigPath -Encoding UTF8
Write-Host "[OK] 创建配置文件: $ConfigPath" -ForegroundColor Green
}
catch {
Write-Error "创建配置文件失败: $($_.Exception.Message)"
exit 1
}
# 配置任务计划程序
Write-Host "正在配置任务计划程序..." -ForegroundColor Yellow
try {
# 删除已存在的任务(如果有)
try {
Unregister-ScheduledTask -TaskName "Smart Network Shutdown Monitor" -Confirm:$false -ErrorAction SilentlyContinue
}
catch {
# 忽略错误,任务可能不存在
}
# 创建触发器(系统启动时)
$Trigger = New-ScheduledTaskTrigger -AtStartup
# 创建操作
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-WindowStyle Hidden -ExecutionPolicy Bypass -File `"$SystemScriptPath`""
# 创建设置
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -DontStopOnIdleEnd -ExecutionTimeLimit (New-TimeSpan -Days 365)
# 创建主体以SYSTEM身份运行
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# 注册任务
Register-ScheduledTask -TaskName "Smart Network Shutdown Monitor" -Trigger $Trigger -Action $Action -Settings $Settings -Principal $Principal -Description "智能网络监控脚本 - 系统级运行"
Write-Host "[OK] 任务计划程序配置完成" -ForegroundColor Green
}
catch {
Write-Error "配置任务计划程序失败: $($_.Exception.Message)"
exit 1
}
# 创建管理脚本
Write-Host "正在创建管理工具..." -ForegroundColor Yellow
$ManagementScript = @"
# -
Write-Host " - " -ForegroundColor Cyan
Write-Host "==============================" -ForegroundColor Cyan
Write-Host ""
Write-Host ":" -ForegroundColor Yellow
Write-Host "1. "
Write-Host "2. "
Write-Host "3. "
Write-Host "4. "
Write-Host "5. "
Write-Host "6. "
Write-Host "7. "
Write-Host ""
`$choice = Read-Host " (1-7)"
switch (`$choice) {
"1" {
Get-ScheduledTask -TaskName "Smart Network Shutdown Monitor" | Format-Table
}
"2" {
Start-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
Write-Host "" -ForegroundColor Green
}
"3" {
Stop-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
Write-Host "" -ForegroundColor Green
}
"4" {
`$logFile = "$LogPath\network_monitor_`$(Get-Date -Format 'yyyyMMdd').log"
if (Test-Path `$logFile) {
Get-Content `$logFile
} else {
Write-Host "" -ForegroundColor Red
}
}
"5" {
`$logFile = "$LogPath\network_monitor_`$(Get-Date -Format 'yyyyMMdd').log"
if (Test-Path `$logFile) {
Get-Content `$logFile -Wait
} else {
Write-Host "" -ForegroundColor Red
}
}
"6" {
Get-Content "$DataPath\config.json"
}
"7" {
notepad "$DataPath\config.json"
}
default {
Write-Host "" -ForegroundColor Red
}
}
Write-Host ""
Write-Host ":" -ForegroundColor Yellow
Write-Host "1. :"
Write-Host " Get-ScheduledTask -TaskName 'Smart Network Shutdown Monitor'"
Write-Host ""
Write-Host "2. :"
Write-Host " Start-ScheduledTask -TaskName 'Smart Network Shutdown Monitor'"
Write-Host ""
Write-Host "3. :"
Write-Host " Stop-ScheduledTask -TaskName 'Smart Network Shutdown Monitor'"
Write-Host ""
Write-Host "4. :"
Write-Host " Get-Content '$LogPath\network_monitor_`$(Get-Date -Format 'yyyyMMdd').log'"
Write-Host ""
Write-Host "5. :"
Write-Host " Get-Content '$LogPath\network_monitor_`$(Get-Date -Format 'yyyyMMdd').log' -Wait"
Write-Host ""
Write-Host "6. :"
Write-Host " Get-Content '$DataPath\config.json'"
Write-Host ""
Write-Host "7. :"
Write-Host " notepad '$DataPath\config.json'"
Write-Host ""
"@
$ManagementScriptPath = Join-Path $ProgramPath "manage.ps1"
try {
$ManagementScript | Set-Content -Path $ManagementScriptPath -Encoding UTF8
Write-Host "[OK] 创建管理脚本: $ManagementScriptPath" -ForegroundColor Green
}
catch {
Write-Error "创建管理脚本失败: $($_.Exception.Message)"
exit 1
}
Write-Host ""
Write-Host "[INFO] 系统级部署完成!" -ForegroundColor Green
Write-Host ""
Write-Host "部署摘要:" -ForegroundColor Cyan
Write-Host "[OK] 程序文件已部署到: $ProgramPath" -ForegroundColor White
Write-Host "[OK] 数据目录已创建: $DataPath" -ForegroundColor White
Write-Host "[OK] 日志目录已创建: $LogPath" -ForegroundColor White
Write-Host "[OK] 任务计划程序已配置(系统启动时运行)" -ForegroundColor White
Write-Host "[OK] SYSTEM 权限配置完成" -ForegroundColor White
Write-Host ""
Write-Host "管理工具:" -ForegroundColor Yellow
Write-Host "- 管理脚本: $ManagementScriptPath" -ForegroundColor White
Write-Host "- 配置文件: $DataPath\config.json" -ForegroundColor White
Write-Host "- 今天日志: $LogPath\network_monitor_$(Get-Date -Format 'yyyyMMdd').log" -ForegroundColor White
Write-Host ""
Write-Host "下次重启后,脚本将自动运行!" -ForegroundColor Green
Write-Host ""
# 询问是否立即启动
$response = Read-Host "是否立即启动监控任务?(Y/N)"
if ($response -eq 'Y' -or $response -eq 'y' -or $response -eq '') {
try {
Start-ScheduledTask -TaskName "Smart Network Shutdown Monitor"
Write-Host "[OK] 监控任务已启动" -ForegroundColor Green
}
catch {
Write-Host "[X] 启动任务失败: $($_.Exception.Message)" -ForegroundColor Red
}
}

0
run_deploy.ps1 Normal file
View File

234
smart_shutdown.ps1 Normal file
View File

@@ -0,0 +1,234 @@
#Requires -Version 5.1
#Requires -RunAsAdministrator
<#
.SYNOPSIS
一个智能网络监控脚本当检测到与目标IP的网络持续中断后会自动关闭计算机。
.DESCRIPTION
该脚本会定期测试与指定目标IP的网络连通性。
如果在设定的“监控窗口”时间内连接持续失败,脚本将启动一个可取消的关机倒计时。
如果在倒计时期间网络恢复,关机将被中止。
所有操作都会被记录在日志文件中。
#>
# ==================== 配置参数 ====================
# 监控的目标IP地址
[string]$TargetIP = "192.168.3.3"
# 正常监控时的ping间隔
[int]$NormalPingInterval = 15
# 监控窗口时长(秒) - 在此期间持续失败则触发关机
[int]$MonitorWindowSeconds = 60
# 关机倒计时时长(秒)
[int]$ShutdownCountdown = 60
# 关机倒计时阶段的ping间隔
[int]$CountdownPingInterval = 3
# ping超时时间
[int]$PingTimeout = 3
# 日志配置
[string]$LogDirectory = Join-Path -Path $PSScriptRoot -ChildPath "logs"
[string]$LogFile = Join-Path -Path $LogDirectory -ChildPath "network_monitor_$(Get-Date -Format 'yyyyMMdd').log"
[int]$MaxLogDays = 30
# =================================================
# --- 函数定义 ---
# 日志写入函数
function Write-Log {
param(
[Parameter(Mandatory=$true)]
[ValidateSet("INFO", "SUCCESS", "FAIL", "WARN", "CRITICAL", "DEBUG")]
[string]$Level,
[Parameter(Mandatory=$true)]
[string]$Message
)
$logTimestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "[$logTimestamp] [$Level] $Message"
# 尝试写入日志文件
try {
if (-not (Test-Path -Path $LogDirectory)) {
New-Item -Path $LogDirectory -ItemType Directory -ErrorAction Stop | Out-Null
}
Add-Content -Path $LogFile -Value $logEntry -ErrorAction Stop
}
catch {
Write-Host "[$logTimestamp] [CRITICAL] 日志文件写入失败!请检查权限或磁盘空间。" -ForegroundColor Red
}
# 根据日志级别在控制台输出不同颜色的信息
switch ($Level) {
"SUCCESS" { Write-Host $logEntry -ForegroundColor Green }
"FAIL" { Write-Host $logEntry -ForegroundColor Red }
"WARN" { Write-Host $logEntry -ForegroundColor Yellow }
"CRITICAL" { Write-Host $logEntry -ForegroundColor DarkRed }
default { Write-Host $logEntry }
}
}
# 获取本机的最佳IP地址
function Get-LocalIP {
param($TargetIP)
try {
# 尝试寻找与目标IP在同一个子网的本机IP地址
$targetSubnet = ($TargetIP.Split('.')[0..2]) -join '.'
$ip = Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred | Where-Object { $_.IPAddress -like "$targetSubnet.*" } | Select-Object -First 1 -ExpandProperty IPAddress
if ($ip) { return $ip }
# 如果找不到则返回任意一个非环回的私有IP地址
$ip = Get-NetIPAddress -AddressFamily IPv4 -AddressState Preferred | Where-Object { $_.IPAddress -notlike "127.0.0.1" -and $_.InterfaceAlias -notlike "Loopback*" } | Select-Object -First 1 -ExpandProperty IPAddress
return $ip
}
catch {
return "未知"
}
}
# --- 脚本主逻辑 ---
# 清理旧日志
if (Test-Path -Path $LogDirectory) {
Get-ChildItem -Path $LogDirectory -Filter "*.log" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$MaxLogDays) } | Remove-Item
}
# 初始化
Clear-Host
$localIP = Get-LocalIP -TargetIP $TargetIP
Write-Log -Level "INFO" -Message "========== 脚本启动 =========="
Write-Log -Level "INFO" -Message "本机IP: $localIP"
Write-Log -Level "INFO" -Message "监控目标: $TargetIP"
Write-Log -Level "INFO" -Message "监控窗口: ${MonitorWindowSeconds}"
Write-Log -Level "INFO" -Message "关机倒计时: ${ShutdownCountdown}"
Write-Log -Level "INFO" -Message "日志文件: $LogFile"
Write-Log -Level "INFO" -Message "脚本已启动,按 Ctrl+C 退出..."
Write-Host "========================================"
# 主循环
$failureStartTime = $null
$normalStatusCounter = 0 # 正常状态计数器,用于定期记录正常运行日志
$normalStatusLogInterval = 24 # 每24次正常检测记录一次日志约6分钟
while ($true) {# 测试网络连接
try {
# 使用兼容性更好的ping方法
$pingResult = Test-Connection -ComputerName $TargetIP -Count 1 -Quiet -ErrorAction SilentlyContinue
$isOnline = $pingResult
} catch {
Write-Log -Level "FAIL" -Message "网络测试出现异常: $($_.Exception.Message)"
$isOnline = $false
} if ($isOnline) {
if ($failureStartTime) {
Write-Log -Level "SUCCESS" -Message "网络连接已恢复。"
Write-Host "[OK] 网络连接已恢复,重置监控窗口" -ForegroundColor Green
$failureStartTime = $null # 重置失败计时器
$normalStatusCounter = 0 # 重置正常状态计数器
} else {
Write-Host "[OK] 网络连接正常" -ForegroundColor Green
# 正常连接时减少日志频率,只定期记录
$normalStatusCounter++
if ($normalStatusCounter -ge $normalStatusLogInterval) {
Write-Log -Level "INFO" -Message "网络连接持续正常(已检测 $normalStatusCounter 次)"
$normalStatusCounter = 0 # 重置计数器
}
}
# 只在控制台显示等待信息,不写入日志文件
Write-Host "等待 ${NormalPingInterval}秒后继续监控..." -ForegroundColor Cyan
Start-Sleep -Seconds $NormalPingInterval
} else {
# 如果网络不通
Write-Log -Level "FAIL" -Message "Ping失败 - 目标: $TargetIP"
if (-not $failureStartTime) {
# 记录首次失败的时间
$failureStartTime = Get-Date
Write-Log -Level "WARN" -Message "网络首次中断,开始进入监控窗口计时。"
} # 计算网络已中断的时间
$failureDuration = (New-TimeSpan -Start $failureStartTime -End (Get-Date)).TotalSeconds
Write-Log -Level "INFO" -Message "网络已持续中断 $([math]::Round($failureDuration)) / ${MonitorWindowSeconds} 秒。"
# 显示详细状态
$remainingTime = $MonitorWindowSeconds - [math]::Round($failureDuration)
Write-Host "监控状态 - 已中断: $([math]::Round($failureDuration))/${MonitorWindowSeconds}秒,剩余: ${remainingTime}" -ForegroundColor Yellow
if ($failureDuration -ge $MonitorWindowSeconds) {
# 持续失败时间超过监控窗口,开始关机倒计时
Write-Log -Level "CRITICAL" -Message "网络持续中断已超过 ${MonitorWindowSeconds} 秒,开始关机流程。"
Write-Host "========================================" -ForegroundColor Yellow
$shutdownCancelled = $false
$countdownEndTime = (Get-Date).AddSeconds($ShutdownCountdown)
while ((Get-Date) -lt $countdownEndTime -and -not $shutdownCancelled) {
$remainingSeconds = [math]::Ceiling(($countdownEndTime - (Get-Date)).TotalSeconds)
if ($remainingSeconds -le 0) { break }
Write-Log -Level "WARN" -Message "距离关机还有 $remainingSeconds 秒... 正在快速检测网络。"
Write-Host "----------------------------------------" -ForegroundColor Yellow
Write-Host "距离关机还有 $remainingSeconds 秒..." -ForegroundColor Yellow
Write-Host "正在快速检测网络连接... 按任意键可手动取消关机" -ForegroundColor Yellow # 在倒计时中再次检测网络
try {
if (Test-Connection -ComputerName $TargetIP -Count 1 -Quiet -ErrorAction SilentlyContinue) {
Write-Log -Level "SUCCESS" -Message "网络在倒计时期间恢复!取消关机。"
Write-Host "========================================" -ForegroundColor Green
Write-Host "[OK] 网络连接已恢复!取消关机操作" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
$failureStartTime = $null
$shutdownCancelled = $true
break
} else {
Write-Host "[X] 网络仍然中断" -ForegroundColor Red
}
}
catch {
Write-Log -Level "WARN" -Message "倒计时期间网络测试出现异常: $($_.Exception.Message)"
Write-Host "[X] 网络测试异常" -ForegroundColor Red
}
# 检查是否有按键输入(手动取消)
$timeout = $CountdownPingInterval
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt $timeout -and -not $shutdownCancelled) {
if ([Console]::KeyAvailable) {
$null = [Console]::ReadKey($true) # 读取按键但不使用
Write-Log -Level "WARN" -Message "用户手动取消了关机操作" Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "[INFO] 用户已手动取消关机,返回监控模式" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
$failureStartTime = $null
$shutdownCancelled = $true
break }
Start-Sleep -Milliseconds 100
}
}
if (-not $shutdownCancelled) {
Write-Log -Level "CRITICAL" -Message "关机倒计时完成,执行系统关机命令。"
try {
# 取消注释下一行以启用实际关机
# Stop-Computer -Force
Write-Host "模拟关机Stop-Computer -Force (为防止意外,此行已注释)" -ForegroundColor Red
Write-Log -Level "CRITICAL" -Message "关机命令已执行(模拟模式)。"
}
catch {
Write-Log -Level "CRITICAL" -Message "关机命令执行失败: $($_.Exception.Message)"
}
# 脚本执行完关机命令后可以退出
exit
}
} else {
# 在监控窗口期内,继续等待
Write-Log -Level "INFO" -Message "等待 ${NormalPingInterval}秒后继续..."
Start-Sleep -Seconds $NormalPingInterval
}
}
}

91
uninstall_system.ps1 Normal file
View File

@@ -0,0 +1,91 @@
#Requires -RunAsAdministrator
<#
.SYNOPSIS
智能网络监控脚本 - 系统卸载脚本
.DESCRIPTION
完全卸载系统级部署的智能网络监控脚本
#>
$ProgramPath = "C:\Program Files\SmartNetworkMonitor"
$DataPath = "C:\ProgramData\SmartNetworkMonitor"
Write-Host "==========================================" -ForegroundColor Red
Write-Host "智能网络监控脚本 - 系统卸载" -ForegroundColor Red
Write-Host "==========================================" -ForegroundColor Red
Write-Host ""
Write-Host "将要删除:" -ForegroundColor Yellow
Write-Host "- 程序目录: $ProgramPath" -ForegroundColor White
Write-Host "- 数据目录: $DataPath (包含日志文件)" -ForegroundColor White
Write-Host "- 任务计划程序条目" -ForegroundColor White
Write-Host "- 事件日志源" -ForegroundColor White
Write-Host ""
$confirmation = Read-Host "确定要继续吗?这将删除所有相关文件和配置 (y/N)"
if ($confirmation -ne 'y' -and $confirmation -ne 'Y') {
Write-Host "卸载已取消" -ForegroundColor Green
exit 0
}
Write-Host ""
Write-Host "正在卸载..." -ForegroundColor Yellow
# 停止并删除任务计划程序
try {
$task = Get-ScheduledTask -TaskName "Smart Network Shutdown Monitor" -ErrorAction SilentlyContinue
if ($task) {
Stop-ScheduledTask -TaskName "Smart Network Shutdown Monitor" -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName "Smart Network Shutdown Monitor" -Confirm:$false
Write-Host "[OK] 任务计划程序已删除" -ForegroundColor Green
}
}
catch {
Write-Host "[X] 删除任务计划程序失败: $($_.Exception.Message)" -ForegroundColor Red
}
# 删除事件日志源
try {
if ([System.Diagnostics.EventLog]::SourceExists("SmartNetworkMonitor")) {
[System.Diagnostics.EventLog]::DeleteEventSource("SmartNetworkMonitor")
Write-Host "[OK] 事件日志源已删除" -ForegroundColor Green
}
}
catch {
Write-Host "[X] 删除事件日志源失败: $($_.Exception.Message)" -ForegroundColor Red
}
# 删除程序目录
try {
if (Test-Path $ProgramPath) {
Remove-Item -Path $ProgramPath -Recurse -Force
Write-Host "[OK] 程序目录已删除: $ProgramPath" -ForegroundColor Green
}
}
catch {
Write-Host "[X] 删除程序目录失败: $($_.Exception.Message)" -ForegroundColor Red
}
# 询问是否删除数据目录(包含日志)
Write-Host ""
$deleteData = Read-Host "是否同时删除数据目录和所有日志文件?(y/N)"
if ($deleteData -eq 'y' -or $deleteData -eq 'Y') {
try {
if (Test-Path $DataPath) {
Remove-Item -Path $DataPath -Recurse -Force
Write-Host "[OK] 数据目录已删除: $DataPath" -ForegroundColor Green
}
}
catch {
Write-Host "[X] 删除数据目录失败: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "[INFO] 数据目录保留: $DataPath" -ForegroundColor Cyan
}
Write-Host ""
Write-Host "[OK] 卸载完成!" -ForegroundColor Green
Read-Host "按任意键退出"