# 安全关机脚本 # 需要管理员权限运行 # 检查是否以管理员身份运行 if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "请以管理员身份运行此脚本!" -ForegroundColor Red Pause # 退出当前非管理员进程 exit 1 } Write-Host "开始执行安全关机流程..." -ForegroundColor Green # 配置参数 $virtualBoxVMs = @("CentOS", "fnOS") # 替换为您的虚拟机名称 $podmanContainers = @("filecodebox") # 替换为您的容器名称 $vboxManagePath = "C:\Users\NAS\Programs\VirtualBox\VBoxManage.exe" # 1. 关闭VirtualBox虚拟机 Write-Host "`n1. 检查并关闭VirtualBox虚拟机..." -ForegroundColor Yellow if (Test-Path $vboxManagePath) { foreach ($vm in $virtualBoxVMs) { Write-Host "检查虚拟机: $vm" -ForegroundColor Cyan # 检查虚拟机状态 $vmStatus = & $vboxManagePath showvminfo $vm --machinereadable 2>$null | Select-String "VMState=" if ($vmStatus) { $state = $vmStatus -replace 'VMState="|"', '' Write-Host "虚拟机 $vm 状态: $state" if ($state -eq "running") { Write-Host "正在关闭虚拟机: $vm" -ForegroundColor Yellow & $vboxManagePath controlvm $vm acpipowerbutton # 等待虚拟机优雅关闭(最多30秒) $timeout = 30 $counter = 0 while (($state -eq "running") -and ($counter -lt $timeout)) { Start-Sleep -Seconds 1 $counter++ $vmStatus = & $vboxManagePath showvminfo $vm --machinereadable 2>$null | Select-String "VMState=" if ($vmStatus) { $state = $vmStatus -replace 'VMState="|"', '' } } if ($state -eq "running") { Write-Host "强制关闭虚拟机: $vm" -ForegroundColor Red & $vboxManagePath controlvm $vm poweroff } else { Write-Host "虚拟机 $vm 已成功关闭" -ForegroundColor Green } } else { Write-Host "虚拟机 $vm 未运行,跳过关闭" -ForegroundColor Gray } } else { Write-Host "未找到虚拟机: $vm" -ForegroundColor Gray } } } else { Write-Host "未找到VirtualBox (路径: $vboxManagePath),跳过虚拟机关闭步骤" -ForegroundColor Gray } # 2. 关闭Podman容器和服务 Write-Host "`n2. 检查并关闭Podman容器和服务..." -ForegroundColor Yellow # 关闭Podman容器 if (Get-Command "podman" -ErrorAction SilentlyContinue) { foreach ($container in $podmanContainers) { Write-Host "检查容器: $container" -ForegroundColor Cyan # 检查容器是否存在且运行中 $containerInfo = podman ps -a --filter "name=$container" --format "{{.Names}}:{{.State}}" if ($containerInfo) { $containerState = $containerInfo.Split(':')[1] Write-Host "容器 $container 状态: $containerState" if ($containerState -eq "running") { Write-Host "正在停止容器: $container" -ForegroundColor Yellow podman stop $container # 检查停止是否成功 Start-Sleep -Seconds 2 $newState = (podman ps -a --filter "name=$container" --format "{{.State}}") if ($newState -eq "exited") { Write-Host "容器 $container 已成功停止" -ForegroundColor Green } else { Write-Host "容器 $container 停止失败" -ForegroundColor Red } } else { Write-Host "容器 $container 未运行,跳过停止" -ForegroundColor Gray } } else { Write-Host "未找到容器: $container" -ForegroundColor Gray } } # 停止所有正在运行的容器(额外安全措施) Write-Host "检查是否有其他运行中的容器..." -ForegroundColor Cyan $runningContainers = podman ps -q --filter "status=running" if ($runningContainers) { Write-Host "停止所有运行中的容器..." -ForegroundColor Yellow podman stop $runningContainers } # 关闭Podman服务(如果使用Podman Machine) Write-Host "检查Podman服务状态..." -ForegroundColor Cyan $podmanService = Get-Service -Name "Podman Machine" -ErrorAction SilentlyContinue if ($podmanService) { if ($podmanService.Status -eq "Running") { Write-Host "正在停止Podman服务..." -ForegroundColor Yellow Stop-Service -Name "Podman Machine" -Force Write-Host "Podman服务已停止" -ForegroundColor Green } else { Write-Host "Podman服务未运行" -ForegroundColor Gray } } } else { Write-Host "未找到Podman,跳过容器关闭步骤" -ForegroundColor Gray } # 3. 执行netsh winsock reset Write-Host "`n3. 执行netsh winsock reset..." -ForegroundColor Yellow try { netsh winsock reset Write-Host "netsh winsock reset 执行成功" -ForegroundColor Green } catch { Write-Host "执行netsh winsock reset时出错: $_" -ForegroundColor Red } Write-Host "`n安全关机流程完成!" -ForegroundColor Green Write-Host "您现在可以安全地关闭计算机。" -ForegroundColor Green # 可选:自动关机(取消注释以下行以启用自动关机) # Write-Host "系统将在10秒后自动关机..." -ForegroundColor Yellow # Start-Sleep -Seconds 10 # Stop-Computer -Force # 暂停以便查看输出结果 Write-Host "`n按任意键退出..." -ForegroundColor Gray $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")