我有一个可用的PS脚本,用于比较两个目录的内容并报告任何丢失的文件或具有不同内容的文件。这就是它目前所做的:
以下是执行所有这些操作的主要代码块。我需要更改/添加的是能够列出我想要在两个服务器之间进行比较的特定路径。例如,假设我希望以下路径用于比较:
我希望每个目录在其他服务器上的对应物之间进行比较。这意味着它会比较D:\Files\Stuff
和server 1
之间的server 2
路径。但是,它不会相互比较路径。这意味着,我 DON' T 希望它将D:\Files\Stuff
与D:\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;}