Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: Eleкtro en 5 Abril 2024, 00:39 am



Título: [APORTE] [PowerShell] RAR.exe | Test RAR Files
Publicado por: Eleкtro en 5 Abril 2024, 00:39 am
El siguiente script, desarrollado en PowerShell y dependiente del programa de terceros WinRAR (rar.exe), sirve para analizar la integridad de los archivos RAR que haya en el directorio donde se ejecute el script.

(http://i.imgur.com/FbiJT9Dl.png) (https://i.imgur.com/FbiJT9D.png)

(http://i.imgur.com/awdD6O3l.png) (https://i.imgur.com/awdD6O3.png)

(http://i.imgur.com/cclfdWVl.png) (https://i.imgur.com/cclfdWV.png)

El script genera un archivo con nombre "RAR_Test_Files.log" en el directorio de trabajo, donde se registran los nombres de archivos de los tests fallidos. En este ejemplo:

RAR_Test_Files.log
Citar
Código:
Failed test with exit code 3: C:\Nueva carpeta\Stupid Invaders (Spanish) (Disc 1)_128mb.rar



Test RAR Files.ps1
Código
  1. <#
  2. ===========================================================================================
  3. |                                                                                         |
  4. |                                        Variables                                        |
  5. |                                                                                         |
  6. ===========================================================================================
  7. #>
  8.  
  9. $rarExecutablePath = "${env:ProgramFiles}\WinRAR\rar.exe"
  10. $recursiveSearch   = $true
  11. $logErrorFilePath  = "$PSScriptRoot\RAR_Test_Files.log"
  12.  
  13. <#
  14. ===========================================================================================
  15. |                                                                                         |
  16. |                                    Functions                                            |
  17. |                                                                                         |
  18. ===========================================================================================
  19. #>
  20.  
  21. function Show-WelcomeScreen {
  22.    Clear-Host
  23.    Write-Host ""
  24.    Write-Host " $($host.ui.RawUI.WindowTitle)"
  25.    Write-Host " +===================================================+"
  26.    Write-Host " |                                                   |"
  27.    Write-Host " | This script will use RAR.exe to test each file in |"
  28.    Write-Host " | the current working directory, and inform about   |"
  29.    Write-Host " | any errors found during the test.                 |"
  30.    Write-Host " |                                                   |"
  31.    Write-Host " +===================================================+"
  32.    Write-Host ""
  33.    Write-Host " Script Settings            " -ForegroundColor DarkGray
  34.    Write-Host " ===========================" -ForegroundColor DarkGray
  35.    Write-Host " RAR Executable Path: $([System.IO.Path]::GetFullPath($rarExecutablePath))" -ForegroundColor DarkGray
  36.    Write-Host " Recursive Search...: $recursiveSearch" -ForegroundColor DarkGray
  37.    Write-Host ""
  38. }
  39.  
  40. function Confirm-Continue {
  41.    Write-Host " Press 'Y' key to continue or 'N' to exit."
  42.    Write-Host ""
  43.    Write-Host " -Continue? (Y/N)"
  44.    do {
  45.        $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  46.        $char = $key.Character.ToString().ToUpper()
  47.        if ($char -ne "Y" -and $char -ne "N") {
  48.            [console]::beep(1500, 500)
  49.        }
  50.    } while ($char -ne "Y" -and $char -ne "N")
  51.    if ($char -eq "N") {Exit(1)} else {Clear-Host}
  52. }
  53.  
  54. function Test-RarFiles {
  55.    Write-Host "Fetching RAR files, please wait..." -ForegroundColor DarkGray
  56.    Write-Host ""
  57.  
  58.    $rarFiles = $null
  59.    if ($recursiveSearch) {
  60.        $rarFiles = Get-ChildItem -LiteralPath "$PSScriptRoot" -Filter "*.rar" -Recurse -File
  61.    } else {
  62.        $rarFiles = Get-ChildItem -LiteralPath "$PSScriptRoot" -Filter "*.rar" -File
  63.    }
  64.  
  65.    $rarFileCount = $rarFiles.Count
  66.    $currentFileCount = 0
  67.  
  68.    If ($rarFileCount -eq 0) {return}
  69.  
  70.    $testSuccessCount = 0
  71.    $testFailCount = 0
  72.  
  73.    $logStream = [System.IO.StreamWriter]::new($logErrorFilePath, $false)
  74.    foreach ($rarFile in $rarFiles) {
  75.        $currentFileCount +=1
  76.        Write-Host "RAR File $currentFileCount of $($rarFileCount):" -ForegroundColor DarkGray
  77.  
  78.        $psi = New-Object System.Diagnostics.ProcessStartInfo
  79.        $psi.FileName = $rarExecutablePath
  80.        $psi.Arguments = "t -idcdn `"$($rarFile.FullName)`""
  81.        $psi.RedirectStandardOutput = $false
  82.        $psi.UseShellExecute = $false
  83.        $psi.CreateNoWindow = $false
  84.  
  85.        $process = [System.Diagnostics.Process]::Start($psi)
  86.        $process.WaitForExit()
  87.        $exitCode = $process.ExitCode
  88.  
  89.        if ($exitCode -eq 0) {
  90.            Write-Host "RAR process exited with code: $exitCode" -ForegroundColor Green
  91.            $testSuccessCount +=1
  92.        } else {
  93.            Write-Warning "RAR process exited with code: $exitCode"
  94.            $logStream.WriteLine("Failed test with exit code $($exitCode): $($rarFile.FullName)")
  95.            $testFailCount +=1
  96.        }
  97.        Write-Host ""
  98.    }
  99.    $logStream.Close()
  100.  
  101.    $SuccessPercent = [string]::Format("{0:F0}", ($testSuccessCount / $currentFileCount) * 100)
  102.    if ($testSuccessCount -eq $currentFileCount) {
  103.        Write-Host "RAR Tests Successful: $testSuccessCount of $currentFileCount ($SuccessPercent%)" -BackgroundColor Black -ForegroundColor Green
  104.    } else {
  105.        Write-Warning "RAR Tests Successful: $testSuccessCount of $currentFileCount ($SuccessPercent%)"
  106.    }
  107.  
  108.    $FailPercent = [string]::Format("{0:F0}", ($testFailCount / $currentFileCount) * 100)
  109.    if ($testFailCount -eq 0) {
  110.        Write-Host "RAR Tests Failed....: $testFailCount of $currentFileCount ($FailPercent%)" -BackgroundColor Black -ForegroundColor Green
  111.    } else {
  112.        Write-Warning "RAR Tests Failed....: $testFailCount of $currentFileCount ($FailPercent%)"
  113.    }
  114.  
  115. }
  116.  
  117. function Show-GoodbyeScreen {
  118.    Write-Host ""
  119.    Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
  120.    Write-Host ""
  121.    Write-Host "Press any key to exit..."
  122.    $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
  123.    Exit(0)
  124. }
  125.  
  126. <#
  127. ===========================================================================================
  128. |                                                                                         |
  129. |                                         Main                                            |
  130. |                                                                                         |
  131. ===========================================================================================
  132. #>
  133.  
  134. [System.Console]::Title = "Test RAR Files - by Elektro"
  135. #[System.Console]::SetWindowSize(150, 45)
  136. [CultureInfo]::CurrentUICulture = "en-US"
  137.  
  138. try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
  139.  
  140. Show-WelcomeScreen
  141. Confirm-Continue
  142. Test-RarFiles
  143. Show-GoodbyeScreen
  144.