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

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.' }
 
נערך לאחרונה ב:

exprexs

Member
התסריט הבא מוודא יושרת העתקה של כל קובץ מועתק בנפרד.

קוד:
$Source = 'D:'
$Destination = 'C:\Users\Ronik\Documents'
$Name = '*.xml'
$Arguments = @('/IS', '/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 RoboCopy Execution Script? (Y/N)'
    $Choice = Read-Host
#
    if ($Choice -eq 'Y' -or $Choice -eq 'y') {
        $RoboCopyExc = @($Source, $Destination, $Name) + $Execute
        & $RoboCopy @RoboCopyExc
        Write-Host 'Robocopy Execution Completed Successfully.'
#
        # Get file lists from source and destination folders
        $SourceFiles = Get-ChildItem -Path $Source -File -Recurse | Select-Object -ExpandProperty Name
        $DestinationFiles = Get-ChildItem -Path $Destination -File -Recurse | Select-Object -ExpandProperty Name
#
        # Compare file lists to find common files based on their names
        $CopiedFiles = Compare-Object -ReferenceObject $SourceFiles -DifferenceObject $DestinationFiles -IncludeEqual -ExcludeDifferent |
                        Where-Object { $_.SideIndicator -eq '==' -and $_.InputObject -like $Name } |
                        Select-Object -ExpandProperty InputObject
#
# Display CopiedFiles list
        if ($CopiedFiles) {
            $CopiedFilesInfo = @()
            $Counter = 1
#
            foreach ($FileName in $CopiedFiles) {
                $SourceFile = Join-Path -Path $Source -ChildPath $FileName
                $DestinationFile = Join-Path -Path $Destination -ChildPath $FileName
                $SourceHash = Get-FileHash -Path $SourceFile -EA:0
                $DestinationHash = Get-FileHash -Path $DestinationFile -EA:0
#
                if ($SourceHash -and $DestinationHash) {
                    if ($SourceHash.Hash -eq $DestinationHash.Hash) {
                     $Result = '      OK   '
                        } else {
                            $Result = '      Error    '     }
                        $FileInfo = [PSCustomObject]@{
                            '-  Number  -' = $Counter
                            '-  Item Mode  -' = (GI $SourceFile).Mode
                            '-  Integrity check  -' = $Result
                            '-  FileName  -' = $FileName
                        }
                        $Counter++
                        $CopiedFilesInfo += $FileInfo  
                }
            }
            if ($CopiedFilesInfo) {
                $CopiedFilesInfo | Format-Table -AutoSize | Out-String -Stream | % { Write-Host $_ -ForegroundColor Yellow -BackgroundColor Black }
            } else {
                Write-Host 'No files were Verified for Integrity.'
}
}
} else {
Write 'No Action Performed.'
}
} else {
Write 'Destination directory does not exist. Aborting.'
}
#
 
נערך לאחרונה ב:
למעלה