Guest User

GPT-4 - Powershell für parallele filehashes

a guest
Apr 17th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. Prompt
  2. Schreibe ein Powershell Script, dass
  3. - dass einen Hash für alle Dateien in einem Ordner generiert
  4. - und in eine TextDatei schreibt
  5. - und multithreading benutzt
  6.  
  7. GPT-4
  8. Hier ist ein PowerShell-Script, das alle gewünschten Anforderungen erfüllt. Es generiert einen Hash für alle Dateien in einem Ordner, schreibt diese Informationen in eine Textdatei und verwendet Multithreading für eine schnellere Verarbeitung:
  9.  
  10. param (
  11. [string]$FolderPath = ".", # Standardwert für den Ordnerpfad ist das aktuelle Verzeichnis
  12. [string]$OutputFile = "hashes.txt", # Standardwert für die Ausgabedatei
  13. [int]$Threads = 5 # Standardwert für die Anzahl der Threads
  14. )
  15.  
  16. function Get-FileHashParallel {
  17. param (
  18. [string]$Path,
  19. [int]$ThreadCount
  20. )
  21.  
  22. $files = Get-ChildItem -Path $Path -File
  23.  
  24. # Job-Script, das die Hash-Berechnung durchführt
  25. $jobScript = {
  26. param ($file)
  27. $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
  28. return [PSCustomObject]@{
  29. Path = $file.FullName
  30. Hash = $hash.Hash
  31. }
  32. }
  33.  
  34. # Erstellen von Runspaces für Multithreading
  35. $runspacePool = [RunspaceFactory]::CreateRunspacePool(1, $ThreadCount)
  36. $runspacePool.Open()
  37.  
  38. $jobs = @()
  39. foreach ($file in $files) {
  40. $job = [powershell]::Create().AddScript($jobScript).AddArgument($file)
  41. $job.RunspacePool = $runspacePool
  42. $jobs += New-Object PSObject -Property @{
  43. File = $file
  44. Pipe = $job
  45. Result = $job.BeginInvoke()
  46. }
  47. }
  48.  
  49. $fileHashes = @()
  50. while ($jobs) {
  51. for ($i = 0; $i -lt $jobs.Count; $i++) {
  52. $job = $jobs[$i]
  53. if ($job.Result.IsCompleted) {
  54. $fileHashes += $job.Pipe.EndInvoke($job.Result)
  55. $job.Pipe.Dispose()
  56. $jobs.RemoveAt($i)
  57. $i--
  58. }
  59. }
  60. }
  61.  
  62. $runspacePool.Close()
  63. $runspacePool.Dispose()
  64. return $fileHashes
  65. }
  66.  
  67. # Hauptausführung
  68. $fileHashes = Get-FileHashParallel -Path $FolderPath -ThreadCount $Threads
  69. $fileHashes | Format-Table -AutoSize | Out-File -FilePath $OutputFile -Encoding utf8
  70.  
  71. Um das Skript mit Ihren eigenen Einstellungen auszuführen, geben Sie die gewünschten Werte für die Parameter $FolderPath, $OutputFile und $Threads an, wenn Sie die Funktion Get-FileHashParallel aufrufen. Zum Beispiel:
  72.  
  73. .\GetFileHashes.ps1 -FolderPath "C:\MeinOrdner" -OutputFile "hashes_mein_ordner.txt" -Threads 10
  74.  
Advertisement
Add Comment
Please, Sign In to add comment