You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Zip密码校验脚本匹配成功后终止并支持CSV导出?

修正后的Zip密码校验脚本(含终止逻辑与CSV导出)

首先修正原脚本的两处基础错误,再实现你需要的终止脚本和结果导出功能:

错误修正说明

  • 变量名不匹配:原脚本中读取密码列表的变量是$passfile,但循环时用了未定义的$passwords,需改为foreach ($password in $passfile)
  • 7z命令参数错误:-p后应拼接当前尝试的单个密码$password,而非整个密码文件变量$passfile

完整修改后的脚本

# 配置路径
$passfile = Get-Content "C:\Users\parveen.kumar\Downloads\passwords.txt"
$7ZipexePath = "C:\Program Files\7-Zip\7z.exe"
$zipFile = "C:\users\parveen.kumar\Downloads\just.zip"
$outputCsvPath = "C:\Users\parveen.kumar\Downloads\zip_password_results.csv"

# 初始化结果存储数组
$results = @()

foreach ($password in $passfile) {
    # 跳过空行(如果密码文件中有空行)
    if ([string]::IsNullOrWhiteSpace($password)) {
        continue
    }

    Write-Host "尝试密码: $password"
    # 执行7z校验命令,隐藏不必要的输出
    & $7ZipexePath "t" $zipFile "-p$password" *> $null

    # 判断密码是否正确
    if (-Not $?) {
        $isCorrect = $false
        Write-Host "$password - 密码错误"
    } else {
        $isCorrect = $true
        Write-Host "$password - 找到正确密码!" -ForegroundColor Red
        # 添加结果到数组
        $results += [PSCustomObject]@{
            Password   = $password
            IsCorrect  = $isCorrect
            Timestamp  = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }
        # 终止循环(如果需要直接退出整个脚本,将break替换为exit)
        break
    }

    # 将当前尝试结果添加到数组
    $results += [PSCustomObject]@{
        Password   = $password
        IsCorrect  = $isCorrect
        Timestamp  = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    }
}

# 导出结果到CSV文件
$results | Export-Csv -Path $outputCsvPath -NoTypeInformation -Encoding UTF8
Write-Host "结果已导出到: $outputCsvPath"

关键功能说明

  1. 终止脚本逻辑:当匹配到正确密码时,执行break终止foreach循环(若需直接退出整个PowerShell进程,替换为exit)
  2. CSV结果导出:
    • 用[PSCustomObject]创建包含密码、验证状态、时间戳的结果对象
    • 循环中收集所有尝试记录,最后通过Export-Csv导出到指定路径,-NoTypeInformation避免生成多余的类型头,-Encoding UTF8确保中文等字符正常显示
  3. 优化处理:添加了跳过空行的逻辑,避免尝试空密码;用*> $null隐藏7z的冗余输出,只保留自定义的提示信息

内容的提问来源于stack exchange,提问作者The Freelancer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 17:10:37