md5 검사로 robocopy를 실행하는 PowerShell 스크립트를 만들었습니다.
잘 작동하지만 일부 디렉토리 또는 파일을 제외하려고하면 robocopy가 제외를 처리하는 반면 해시를 비교하는 스크립트의 MD5 부분이 작동하지 않고 소스에 대상보다 많은 파일 / 해시가 있기 때문에 일부 오류를 반환합니다. .
아마도 여기와 인터넷에서 찾은 모든 방법을 시도해 보았습니다! 경로에서 dirs 및 / 또는 파일을 제외 할 수 없습니다!
아래는 내가 지금까지 한 일입니다. 이 모드에서는 md5-copy가 작동합니다 (예외 없음).
$Source = "F:\"
$IgnoreDir = @(
$Source + '$RECYCLE.BIN'
$Source + "System Volume Information"
$Source + "VMs"
)
$IgnoreFile = @(
$Source + "SHDrive.vmdk"
$Source + "SHDrive-flat.vmdk"
)
$Ignored = $IgnoreDir + $IgnoreFile
Robocopy :
Robocopy.exe /R:1 /W:0 $Source $Dest /E /V /TEE /XD $IgnoreDir /XF $IgnoreFile /LOG:$LogDir\RBCY_MD5_F.txt
MD5 :
$SourceHash = Get-ChildItem "$Source\*.*" -Recurse -Force -Exclude $Ignored | Where-Object {!$_.psiscontainer } | Get-FileHash
$SourceHash | Select-Object "Hash", "path" | ft -HideTableHeaders -AutoSize | Out-File -Width "300" $LogDir\SRC_MD5_REF.txt
$SourceHash.Hash | Out-File $LogDir\SRC_MD5.txt
비교 :
$Diff = Compare-Object -ReferenceObject $(get-content "$LogDir\SRC_MD5.txt") -DifferenceObject $(get-content "$LogDir\DST_MD5.txt")
F : \ 드라이브의 내용 :
PS C:\Users\Robbi> Get-ChildItem F:\ -force
Directory: F:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d--hs- 19/03/2019 06:40 $RECYCLE.BIN
d----- 16/05/2020 04:41 DATA
d----- 19/01/2020 06:34 Drivers
d----- 16/05/2020 04:55 Gumball
d----- 16/05/2020 04:58 SW
d--hs- 19/03/2019 06:36 System Volume Information
d----- 13/03/2020 16:08 Tools
d----- 12/12/2019 00:02 VMs
d----- 16/05/2020 04:55 _Pre-Cestino
-a---- 08/02/2020 03:02 21474836480 SHDrive-flat.vmdk
-a---- 08/02/2020 03:02 466 SHDrive.vmdk
get-children 목록에서 복사하지 않으려는 데이터를 제외하려면 어떻게해야합니까? 이 특정 경우 및 가능한 경우 Get-ChildItem이 전체 파일 시스템에서 명시 적 콘텐츠 목록 (가변 문자열 및 / 또는 배열)을 제외해야하는 "모든 경우".
PowerShell 7.0부터 -Exclude
와 -Include
같은 cmdlet 의 및 공급자 매개 변수는 전체 경로가 아닌 Get-ChildItem
항목 이름 (파일 시스템 공급자의 경우 파일 / 디렉터리 이름) 에서만 작동 합니다 .
제외하려는 모든 경로가 대상 디렉토리의 직계 자식 이라는 점을 감안할 때 2 단계 접근 방식을 제안합니다.
# Get all files and directories in $Source, except those to be excluded.
# Note the use of \* instead of \*.*, so as to also include the
# directories (whose names don't have an extension).
$items = Get-Item $Source\* -Force | Where-Object FullName -NotIn $Ignored
# Recursively process all resulting files and directories and
# calculate their hashes.
# Note the use of -File to limit output to files.
$SourceHash = $items | Get-ChildItem -Recurse -Force -File | Get-FileHash
물론 $Ignored
파일 / 디렉토리 이름으로 만 배열 을 정의하는 경우 다음을 사용할 수 있습니다 -Exclude
.
# Convert the ignore list to file/directory names only.
$Ignored = $Ignored | Split-Path -Leaf
$SourceHash = Get-ChildItem -File $Source -Recurse -Force -Exclude $Ignored |
Get-FileHash
제외 할 경로가 하위 디렉토리 계층의 모든 수준에서 발생할 수있는 경우 추가 작업이 필요합니다.
$ignoredRegex = '^(?:{0})(?:\{1}|$)' -f
($Ignored.ForEach({ [regex]::Escape($_) }) -join '|'),
[IO.Path]::DirectorySeparatorChar
$SourceHash = Get-ChildItem $Source -Recurse -File -Force |
Where-Object FullName -NotMatch $ignoredRegex
Get-FileHash
위는 연산자 와 함께 정규식을 사용 하여 하위 디렉토리 트리의 -match
모든 지정된 경로 와 해당 하위 를 제외합니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다