循环遍历CSV文件

时间:2018-05-15 17:55:54

标签: powershell

我有一个可用的PS脚本,用于比较两个目录的内容并报告任何丢失的文件或具有不同内容的文件。这就是它目前所做的:

  1. 采用两个特定目录。
  2. 从每个目录中拉出所有文件(减去任何排除的路径/​​文件)。
  3. 检查一个或另一个文件是否缺少任何文件。
  4. 对于两个目录中的每个文件,它会对该文件的内容进行哈希比较。
  5. 根据源中的文件而不是目标(反之亦然)将结果放在变量中,以及源和目标中但具有不同内容的文件。
  6. 以下是执行所有这些操作的主要代码块。我需要更改/添加的是能够列出我想要在两个服务器之间进行比较的特定路径。例如,假设我希望以下路径用于比较:

    • d:\文件\东西
    • d:\文件\内容\文件夹\ MoreStuff
    • d:\文件\归档

    我希望每个目录在其他服务器上的对应物之间进行比较。这意味着它会比较D:\Files\Stuffserver 1之间的server 2路径。但是,它不会相互比较路径。这意味着,我 DON' T 希望它将D:\Files\StuffD:\Files\Archive进行比较,而不管服务器是什么。

    我能做到这一点的最佳方式是什么?

    $SourceDir = "\\12345-serverP1\D$\Files";
    $DestDir = "\\54321-serverP2\D$\Files";
    
    #The excluded array holds all of the specific paths, files, and file types you don't want to be compared.
    $ExcludedPaths = @(Import-Csv -LiteralPath 'D:\ExclusionList.csv') |Select-Object -Expand ExcludedPaths;
    $ExcludedFiles = @(Import-Csv -LiteralPath 'D:\ExclusionList.csv') |Select-Object -Expand ExcludedFiles;
    
    #Script block which stores the filter for the Where-Object used to exclude chosen paths.
    #This script is called with the -FilterScript parameter below.
    $Filter = {
        $FullName = $_.FullName
        -not($ExcludedPaths | Where-Object {$FullName -like "$_*";})
    }
    
    #Grabs all files from each directory, minus the excluded paths and files, and assigns them to variables based on Source and Destination.
    try {$SourceFiles = Get-ChildItem -Recurse -Path $SourceDir -Exclude $ExcludedFiles -Force -ErrorAction Stop | Where-Object -FilterScript $Filter;}
    catch {Write-Output  "$(Get-Date) The following Source path was not found: $SourceDir" | Out-File $ErrorLog -Append;}
    
    try {$DestFiles = Get-ChildItem -Recurse -Path $DestDir -Exclude $ExcludedFiles -Force -ErrorAction Stop | Where-Object -FilterScript $Filter;}
    catch {Write-Output  "$(Get-Date) The following Destination path was not found: $DestDir" | Out-File $ErrorLog -Append;}
    
    #Pulls the name of each file and assigns it to a variable.
    $SourceFileNames = $SourceFiles | % { $_.Name };
    $DestFileNames = $DestFiles | % { $_.Name };
    
    #Empty variables to be used in the loops below.
    $MissingFromDestination = @();
    $MissingFromSource = @();
    $DifferentFiles = @();
    $IdenticalFiles = @();
    
    #Iterates through each file in the Source directory and compares the name against each file in the Destination directory.
    #If the file is missing from the Destination, it is added to the MissingFromDestination variable. 
    #If the file appears in both directories, it compares the hash of both files. 
    #If the hash is the same, it adds it to the IdenticalFiles variable. If the hash is different, it adds the Source file to the DifferentFiles variable.
    try {
        foreach ($f in $SourceFiles) {
            if (!$DestFileNames.Contains($f.Name)) {$MissingFromDestination += $f;}
            elseif ($DestFileNames.Contains($f.Name)) {$IdenticalFiles += $f;}
            else {
                $t = $DestFiles | Where { $_.Name -eq $f.Name };
                if ((Get-FileHash $f.FullName).hash -ne (Get-FileHash $t.FullName).hash) {$DifferentFiles += $f;}
            }
        }
    }
    catch {Write-Output  "$(Get-Date) The Destination variable is null due to an incorrect path." | Out-File $ErrorLog -Append;}
    
    #Iterates through each file in the Destination directory and compares the name against each file in the Source directory.
    #If the file is missing from the Source, it is added to the MissingFromSource variable.
    try {
        foreach ($f in $DestFiles) {
            if (!$SourceFileNames.Contains($f.Name)) {$MissingFromSource += $f;}
        }
    }
    catch {Write-Output  "$(Get-Date) The Source variable is null due to an incorrect path." | Out-File $ErrorLog -Append;}
    

0 个答案:

没有答案