2013-11-15 5 views
1

오케이 - 전 PowerShell을 처음 접했습니다. 2 주 전에 만 사용하기 시작했습니다. 나는 스크립트를 작성하기 위해 웹을 샅샅이 뒤 졌는데, 지금은 약간 진보 된 것처럼 보이는 것을 시도하고 있는데 어떻게 해결해야할지 불확실하다.큰 개체로 인해 Get-ChildItem 스크립트가 중단됩니까?

2 개의 백업 리포지토리간에 서로 다른 파일이 있는지 확인하기 위해 감사 스크립트를 만들어 동기화 스크립트가 robocopy를 사용하며 오류가 발생하지 않고 두 번 이상 실패했습니다. 폴더는 매우 광범위하며 경우에 따라 스크립트가 특정 폴더 (항상 가장 큰 폴더)에 매달려 있으며이 때문에 완료되지 않을 것입니다.

처음에는 전체 소스 경로에서 Get-ChildItem을 사용했지만 메모리 문제가 발생하여 스크립트가 완료되지 않습니다. 그래서, 나는 하위 디렉토리를 열거하고 각 하위 디렉토리에서 비교를 수행 할 것이라고 생각했지만 ... 폴더에 따라 다르다. 여기

은 (파워 쉘 2를 사용하여) 스크립트입니다 : 대화 형으로 실행 내가받을거야 때 내가 발견 한 무엇

[email protected]("Server1","Server2","Server3") 

for ($i=0; $i -lt 8; $i++) { 

    $server = $serverArray[$i] 
    $source="\\$server\Share\" 

    $destination = "D:\BackupRepository\$server" 
    # Copy to removable drive 
    $remoteDestination = "T:\BackupRepository\" + $server 

    $log = $server + "ShareBackup.log" 
    $remoteLog = "Remote_" + $server + "ShareBackup.log" 
    $logDestination = $localLogPath + $log 
    $logUNCDestination = $uncLogPath + $log 
    $logRemoteDestination = $localLogPath + $remoteLog 
    $logUNCRemoteDestination = $uncLogPath + $remoteLog 

    ## This file is used for the process of checking 
    ## whether or not the backup was successful 
    $backupReport = $localReportPath + $server + "ShareBackupReport.txt" 
    $remoteBackupReport = $localReportPath + "Remote_" + $server + "ShareBackupReport.txt" 

    ## Variables for the failure emails 
    $failEmailSubject = "AUDIT REPORT for " + $server 
    $failRemoteEmailSubject = "AUDIT REPORT for " + $server 
    $failEmailBody = "The Audit for " + $server + " has found a file mismatch. Please consult the attached Backup Report." 
    $failRemoteEmailBody = "The Audit of the Remote Backups for " + $server + " has found a file mismatch. Please consult the attached Backup Report." 

    $sourceFolderArray = Get-ChildItem $source | ?{ $_.PSIsContainer } 
    $sourceFolderCount = $sourceFolderArray.Count 
    $mismatchCount = 0 
    $remoteMismatchCount = 0 

    for ($s1=0; $s1 -lt $sourceFolderCount; $s1++) { 

     $sourceFolder = $sourceFolderArray[$s1].FullName 
     $sourceFolderName = $sourceFolderArray[$s1].Name 
     $destFolder = $destination + "\" + $sourceFolderName 
     $remoteDestFolder = $remoteDestination + "\" + $sourceFolderName 

     Write-Host "Currently working on: " $sourceFolderName 

     $shot1 = Get-ChildItem -recurse -path $sourceFolder 
     $shot2 = Get-ChildItem -recurse -path $destFolder 
     $shot3 = Get-ChildItem -recurse -path $remoteDestFolder 

     $auditReportDest = "C:\BackupReports\Audits\" 
     $auditReportOutput = $auditReportDest + $server + "_" + $sourceFolderName + ".txt" 
     $auditReportRemoteOutput = $auditReportDest + $server + "_Remote_" + $sourceFolderName + ".txt" 
     $auditMismatchReport = $auditReportDest + "MismatchReport_" + $numericDate + ".txt" 

     Compare-Object $shot1 $shot2 -PassThru > $auditReportOutput 
     Compare-Object $shot2 $shot3 -PassTHru > $auditReportRemoteOutput 

     $auditCompare = Get-ChildItem $auditReportOutput 
     $auditRemoteCompare = Get-ChildItem $auditReportRemoteOutput 

     if ($auditCompare.Length -gt 0) { 
      $content = Get-ChildItem -Recurse $auditReportOutput 
      Add-Content $auditMismatchReport $content 
      Write-Host "Mismatch FOUND: " $sourceFolderName 
      $mismatchCount = $mismatchCount + 1 
      } 
     if ($auditRemoteCompare.Length -gt 0) { 
      $remoteContent = Get-ChilItem -Recurse $auditReportRemoteOutput 
      Add-Content $auditMismatchReport $remoteContent 
      Write-Host "Remote Mismatch FOUND: " $sourceFolderName 
      $remoteMismatchCount = $remoteMismatchCount + 1 
      } 

     } 

    send-mailmessage -from $emailFrom -to $emailTo -subject "AUDIT REPORT: Backups" -body "The full mismatch report is attached. There were $mismatchCount mismatched folders found and $remoteMismatchCount remote mismatched folders found. Please review to ensure backups are current." -Attachments "$auditMismatchReport" -priority High -dno onSuccess, onFailure -smtpServer $emailServer 

    } 

a와 "현재 FolderName은 작업"그 목적은 "너무 큰 경우 "(그게 뭐든간에) 스크립트는 오류의 징후가없는 지점에 그냥 앉아 있지만 계속되지는 않습니다 (몇 시간을 기다렸습니다). 때로는 Ctrl-C를 대화식으로 눌러서 스크립트를 종료하는 대신 인터럽트를 현재 프로세스의 취소로 가져와 다음 항목으로 이동합니다.

필자는 백업을 동기화 된 상태로 유지하기 위해 매일 발생하도록 예약해야합니다. 어떤 도움이나 통찰력도 환영합니다. 그리고 네, 아마도 원시적이고 우아하지 않을 수 있습니다.하지만 지금은 저에게 매달려있는 스크립트를 어떻게 풀 수 있는지 해결하려고하고 있습니다. 큰 디렉토리로 확장

+0

방금 ​​파일 이름을 확인하고 있습니까, 아니면 길이가 맞는지 확인 중입니까? – mjolinor

+0

파일 이름이 존재하는지 확인하기 만하면됩니다. 충분한 파일이 매일 바뀌기 때문에 필요한 모든 것입니다. – doulos2k

답변

1

사용중인 PS의 버전을 확인하지만-ChildItem을 얻을 알려져있다되지 않음 문제 : 당신은 그냥 파일 이름을 비교하는 경우

http://blogs.msdn.com/b/powershell/archive/2009/11/04/why-is-get-childitem-so-slow.aspx

, 당신은 훨씬 더 얻을 수 있습니다 레거시 dir 명령을 사용하여 큰 디렉토리 구조가 생성됩니다./b (bare) 스위치는 Powershell의 비교 연산자에서 쉽게 사용할 수있는 전체 이름 문자열을 반환합니다.

$sourcedir = 'c:\testfiles' 
$source_regex = [regex]::escape($sourcedir) 

(cmd /c dir $Sourcedir /b /s) -replace "$source_regex\\(.+)$",'$1' 

이 정규 표현식과 디렉토리에 의해 반환 된 fullnames의 오프 soruce 디렉토리를 트림하는 -replace 연산자를 사용합니다. -replace 연산자는 배열과 함께 작동하므로 foreach 루프없이 한 번에 모든 작업을 수행 할 수 있습니다.

+0

Powershell 2.0 사용 - 죄송합니다. 나는 그것을 게시 했어야했다 ... 그것은 태그에 있었지만, 거의 보이지 않았다. 내 잘못이야. 나는 dir을 조사 할 것이다 ... 나는 재귀 적 시나리오에서 그것을 사용할 수 있다는 것을 몰랐다. – doulos2k

+0

죄송합니다, 태그를 놓쳤습니다. 이 기사의 GPI 성능 문제는 V2에도 적용됩니다. – mjolinor

+0

우선 시간을내어 주셔서 감사합니다. 감사합니다. 그래서, 그것은 확실히 진전입니다. 문제는 두 개의 완전히 다른 디렉토리 구조를 비교하기 때문에/b 스위치는 완전한 경로를 제공합니다 ... 그래서 Compare-Object는 항상 전체 경로를 생성하기 때문에 전체 목록을 생성합니다). Compare-Object에서 기본 경로를 무시하도록 할 수 있습니까? 아니면 dir 명령이 기본 경로 이외의 모든 것을 출력하도록 할 수 있습니까? – doulos2k