Skip to content

DLL加载失败: DLL中未找到类 SeBackupPrivilege.PrivilegeUtils #1

Description

@QGB
# 文件名: Copy-EdgeCookies.ps1
# 路径: C:\test\win10\
# 需管理员权限运行

# ---------- 配置参数 ----------
$DllDir = Join-Path -Path $PSScriptRoot -ChildPath "SeBackupPrivilege"
$Source = Join-Path -Path $env:USERPROFILE -ChildPath "AppData\Local\Microsoft\Edge\User Data\Default\Network\Cookies"
$Destination = Join-Path -Path $PSScriptRoot -ChildPath "Cookies_Copy"

# ---------- 检查DLL文件 ----------
$requiredDlls = @(
    "$DllDir\SeBackupPrivilegeCmdLets.dll",
    "$DllDir\SeBackupPrivilegeUtils.dll"
)
foreach ($dll in $requiredDlls) {
    if (-not (Test-Path $dll)) {
        Write-Host "[错误] 文件缺失: $($dll)" -ForegroundColor Red
        exit
    }
}

# ---------- 加载DLL类型 ----------
try {
    # 关键修正:显式指定类名
    Add-Type -Path $requiredDlls[1]  # 先加载Utils.dll
    Add-Type -Path $requiredDlls[0]  # 再加载CmdLets.dll

    # 验证类是否存在
    if (-not ("SeBackupPrivilege.PrivilegeUtils" -as [Type])) {
        throw "DLL中未找到类 SeBackupPrivilege.PrivilegeUtils"
    }
    Write-Host "[√] DLL加载成功" -ForegroundColor Green
} catch {
    Write-Host "[错误] DLL加载失败: $_" -ForegroundColor Red
    exit
}

# ---------- 激活备份特权 ----------
try {
    # 关键修正:使用完整命名空间
    [SeBackupPrivilege.PrivilegeUtils]::EnableBackupPrivilege()
    Write-Host "[√] SeBackupPrivilege权限已激活" -ForegroundColor Green
} catch {
    Write-Host "[错误] 权限激活失败: $_" -ForegroundColor Red
    exit
}

# ---------- 通过卷影副本复制 ----------
try {
    # 生成卷影脚本
    $vssScript = @"
set context persistent nowriters
add volume C: alias EdgeVSS
create
expose %EdgeVSS% Z:
"@
    $vssScriptPath = Join-Path -Path $env:TEMP -ChildPath "vss_$(Get-Date -Format 'yyyyMMddHHmmss').dsh"
    $vssScript | Out-File -FilePath $vssScriptPath -Encoding ASCII

    # 创建卷影
    Write-Host "[*] 正在创建卷影副本..." -ForegroundColor Cyan
    diskshadow /s $vssScriptPath | Out-Null

    # 复制文件
    $vssSource = $Source.Replace("C:", "Z:")
    Write-Host "[*] 正在复制: $vssSource → $Destination" -ForegroundColor Cyan
    Copy-FileSeBackupPrivilege -SourcePath $vssSource -DestinationPath $Destination -Force

    # 清理卷影
    $cleanupScript = @"
set context persistent nowriters
delete shadows volume %EdgeVSS%
reset
"@
    $cleanupScript | diskshadow /s | Out-Null
} catch {
    Write-Host "[错误] 复制失败: $_" -ForegroundColor Red
    exit
}

# ---------- 验证结果 ----------
if (Test-Path $Destination) {
    Write-Host "[√] 文件已保存到: $Destination" -ForegroundColor Green
    # 哈希校验
    try {
        $srcHash = (Get-FileHash $Source -Algorithm SHA256 -ErrorAction SilentlyContinue).Hash
        $destHash = (Get-FileHash $Destination -Algorithm SHA256).Hash
        if ($srcHash -eq $destHash) {
            Write-Host "[√] SHA256校验通过: $($srcHash.Substring(0,8))..." -ForegroundColor Green
        } else {
            Write-Host "[!] 警告: 文件哈希不匹配 (可能源文件被锁定)" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "[!] 哈希校验跳过: $_" -ForegroundColor Yellow
    }
} else {
    Write-Host "[错误] 文件未生成" -ForegroundColor Red
}

# ---------- 关闭特权 ----------
[SeBackupPrivilege.PrivilegeUtils]::DisableBackupPrivilege()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions