Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Prompt
- Schreibe ein Powershell Script, dass
- - dass einen Hash für alle Dateien in einem Ordner generiert
- - und in eine TextDatei schreibt
- - und multithreading benutzt
- GPT-4
- 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:
- param (
- [string]$FolderPath = ".", # Standardwert für den Ordnerpfad ist das aktuelle Verzeichnis
- [string]$OutputFile = "hashes.txt", # Standardwert für die Ausgabedatei
- [int]$Threads = 5 # Standardwert für die Anzahl der Threads
- )
- function Get-FileHashParallel {
- param (
- [string]$Path,
- [int]$ThreadCount
- )
- $files = Get-ChildItem -Path $Path -File
- # Job-Script, das die Hash-Berechnung durchführt
- $jobScript = {
- param ($file)
- $hash = Get-FileHash -Path $file.FullName -Algorithm SHA256
- return [PSCustomObject]@{
- Path = $file.FullName
- Hash = $hash.Hash
- }
- }
- # Erstellen von Runspaces für Multithreading
- $runspacePool = [RunspaceFactory]::CreateRunspacePool(1, $ThreadCount)
- $runspacePool.Open()
- $jobs = @()
- foreach ($file in $files) {
- $job = [powershell]::Create().AddScript($jobScript).AddArgument($file)
- $job.RunspacePool = $runspacePool
- $jobs += New-Object PSObject -Property @{
- File = $file
- Pipe = $job
- Result = $job.BeginInvoke()
- }
- }
- $fileHashes = @()
- while ($jobs) {
- for ($i = 0; $i -lt $jobs.Count; $i++) {
- $job = $jobs[$i]
- if ($job.Result.IsCompleted) {
- $fileHashes += $job.Pipe.EndInvoke($job.Result)
- $job.Pipe.Dispose()
- $jobs.RemoveAt($i)
- $i--
- }
- }
- }
- $runspacePool.Close()
- $runspacePool.Dispose()
- return $fileHashes
- }
- # Hauptausführung
- $fileHashes = Get-FileHashParallel -Path $FolderPath -ThreadCount $Threads
- $fileHashes | Format-Table -AutoSize | Out-File -FilePath $OutputFile -Encoding utf8
- 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:
- .\GetFileHashes.ps1 -FolderPath "C:\MeinOrdner" -OutputFile "hashes_mein_ordner.txt" -Threads 10
Advertisement
Add Comment
Please, Sign In to add comment