怎么用脚本获取系统恢复环境

wen 实用脚本 3

本文目录导读:

怎么用脚本获取系统恢复环境

  1. 方法一:PowerShell脚本(推荐)
  2. 方法二:CMD批处理脚本
  3. 方法三:高级信息获取脚本
  4. 使用说明:
  5. 注意事项:

PowerShell脚本(推荐)

# 获取Windows恢复环境信息
$reAgent = reagentc /info 2>&1
Write-Host "Windows RE 状态信息:" -ForegroundColor Green
$reAgent
# 结构化输出
try {
    $info = Get-WindowsRecoveryEnvironment 2>$null
    if ($info) {
        Write-Host "`n详细配置:" -ForegroundColor Cyan
        $info | Format-List
    }
} catch {
    Write-Warning "无法获取详细信息,请以管理员身份运行"
}
# 检测RE是否启用
if (reagentc /info | Select-String "Enabled") {
    Write-Host "`n状态: Windows RE 已启用" -ForegroundColor Green
} else {
    Write-Host "`n状态: Windows RE 未启用或不可用" -ForegroundColor Yellow
}

CMD批处理脚本

@echo off系统恢复环境检查
color 0A
echo ========================================
echo      Windows RE 恢复环境信息
echo ========================================
echo.
REM 检查管理员权限
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo 请以管理员身份运行此脚本
    pause
    exit /b 1
)
REM 获取RE信息
echo 正在获取恢复环境配置...
echo.
reagentc /info
echo.
echo ----------------------------------------
echo 恢复分区信息:
REM 查找WinRE分区
diskpart /s "%~f0" 2>nul || (
    echo 正在扫描恢复分区...
    wmic path Win32_LogicalDisk where "VolumeName='WinRE'" get DeviceID,Size /format:list 2>nul
)
pause

高级信息获取脚本

# 完整的Windows RE信息采集脚本
param(
    [switch]$Export,
    [string]$OutputPath = "$env:TEMP\WinRE_Report.txt"
)
# 获取详细RE信息
function Get-WinREInfo {
    $result = [PSCustomObject]@{
        Enabled = $false
        Location = ""
        Description = ""
        PartitionInfo = $null
    }
    # 使用reagentc获取基本信息
    $info = reagentc /info 2>&1
    $result.Description = $info -join "`n"
    # 检测是否启用
    if ($info -match "Windows RE status.*Enabled") {
        $result.Enabled = $true
    }
    # 获取位置信息
    if ($info -match "Windows RE location: (.*)") {
        $result.Location = $matches[1]
    }
    # 获取恢复分区信息
    $result.PartitionInfo = Get-Partition -DriveLetter $([char]($result.Location.Split(':')[0])) -ErrorAction SilentlyContinue
    return $result
}
# 主程序
$reInfo = Get-WinREInfo
Write-Host "=== Windows 恢复环境信息 ===" -ForegroundColor Cyan
Write-Host "状态: $(if($reInfo.Enabled){'已启用'}else{'未启用'})" -ForegroundColor $(if($reInfo.Enabled){'Green'}else{'Yellow'})
Write-Host "位置: $($reInfo.Location)"
if ($reInfo.PartitionInfo) {
    Write-Host "分区信息:"
    $reInfo.PartitionInfo | Select-Object DriveLetter, Size, Offset, PartitionNumber | Format-Table -AutoSize
}
# 导出选项
if ($Export) {
    $reInfo | Export-Clixml -Path $OutputPath
    Write-Host "报告已导出到: $OutputPath" -ForegroundColor Green
}

使用说明:

  1. 保存脚本

    • PowerShell脚本保存为 .ps1 文件
    • CMD脚本保存为 .bat.cmd 文件
  2. 运行要求

    • 必须以管理员权限运行
    • Windows 10/11 专业版/企业版
  3. 常见输出示例

    Windows RE status: Enabled
    Windows RE location: \\?\GLOBALROOT\device\harddisk0\partition4\Recovery\WindowsRE
    Boot Configuration Data (BCD) identifier: {current}
    Recovery image location: 

注意事项:

  • 如果显示“Disabled”,可以使用 reagentc /enable 启用
  • 在Windows 11中,恢复环境通常位于隐藏的恢复分区
  • 某些企业版系统可能使用自定义配置

根据你的具体需求(信息获取、状态监测、故障排查等),选择相应的方法,如果只需要快速检查状态,使用CMD方法即可;如果需要详细的结构化信息,推荐PowerShell脚本。

抱歉,评论功能暂时关闭!