העתק קבצים, וודא יושרת ההעתק

exprexs

Member
תסריט הבא, מבצע העתקת קבצים ומוודא שכל העתק בנפרד, "נאמן למקור".
התסריט בודק תחילה אם תיקיית היעד קיימת בכלל. ( אם לא התסריט מסתיים ).
התסריט מציג רשימת קבצים רצויים לפני העתקה ושואל אם להמשיך ( אם לא התסריט מסתיים ).
אם התשובה חיובית, התסריט מבצע העתקה.
בנוסף, התסריט מבצע hashing לכל קובץ מקור וכל קובץ יעד בנפרד ובודק אותם זה מול זה ( אלגוריתם המחדל, SHA256 ).
אם הכל נמצא נאמן למקור, התסריט מודיע על כך.
אם התסריט מוצא קובץ כלשהו שאינו נאמן למקור, התסריט מפרט אודותיו בשמו.


Integrate RoboCopy in PowerShell.
Check the copied files for integrity by hash check.


קוד:
# Copy Items, Single Source. Test-Path & Hash
$Source = 'D:'
$Destination ='D:\Drill_Folder'
$Name = '*.txt'
$Arguments = @('/XO', '/XD', '*', '/R:0', '/W:0', '/MT', '/E', '/A-:SH', '/L')
$Execute = $Arguments | ? {$_ -ne '/L'}
$RoboCopy = 'C:\Windows\System32\Robocopy.exe'
$Check = Test-Path -Path $Destination -PathType 'Container'
#
if ($Check -eq $true) {
$RoboCopyArgs = @($Source, $Destination, $Name) + $Arguments
& $RoboCopy @RoboCopyArgs
Write-Host 'Would you like to continue with the Robocopy execution script? (Y/N)'
$Choice = Read-Host
if ($Choice -eq 'Y' -or $Choice -eq 'y') {
$RoboCopyArgs = @($Source, $Destination, $Name) + $Execute
& $RoboCopy @RoboCopyArgs
Write-Host 'Robocopy Execution Completed.'
#
# Source Hash, # Creating a list, # Destination Hash, # Add copied Files
$CopiedFiles = @()
$CopiedFiles += Get-ChildItem -Path $Destination -File
$SourceFiles = Get-ChildItem -Path $Source -File
$SourceHash = $SourceFiles | % { Get-FileHash $_.FullName }
$DestinationFiles = Get-ChildItem -Path $Destination -File
$DestinationHash = $DestinationFiles | % { Get-FileHash $_.FullName }
#
# Integ Check
Foreach ($file in $CopiedFiles) {
$SourceFile = $file -replace [regex]::Escape($Destination), [regex]::Escape($Source)
$SourceHashValue = $SourceHash | ? { $_.Path -eq $SourceFile }
$DestinationHashValue = $DestinationHash | ? { $_.Path -eq $file }
if ($SourceHashValue.Hash -ne $DestinationHashValue.Hash) {
Write-Host "Error: '$file' Differs from the source." }}
Write-Host 'File Integrity Check Completed Successfully.'} Else {
Write-Host 'No Action Performed.' }} else {
Write-Host 'Destination directory does not exist. Aborting.' }
 
נערך לאחרונה ב:
למעלה