I am having problems with implementing the logic I need to compare file contents in different directory locations. Production will involve mapping a drive as the Target path and comparing the Source to mapped Target.
I need to create a script that will accept two parameters
- Source Path
- Target Path
- Not in Target but in the Source
- Not in Source but in the Target
- If there are Object Files in both Source and Target, indicate if the contents are the same.
This is what I have so far:
#Get Source and Target Objects and get the differences:
$SourceObjects = Get-ChildItem -Recurse C:\utility\Source\ $Targetobjects = Get-ChildItem -Recurse C:\utility\Destination\ $ObjectResults = Compare-Object -ReferenceObject $SourceObjects -DifferenceObject $TargetObjects -IncludeEqual
Here are my $ObjectResults at this point.
InputObject SideIndicator ----------- ------------- SubFolder == Duplicate.txt == Hash.txt == UserCheck.exe == UserChoice.exe == UserNote.exe == Duplicate.txt == Test.txt <=
Now at this point, with this information, I have criteria 1 and 2 met. I know that the Source file c:\utility\Source\Test.txt is in the Source Directory and not the c:\utility\Destination directory. All the other files are the same as far as file name. I need to now compare these files by hashing each file and comparing the hash values with the corresponding Target File Objects.
Now I create a Hash Variable of my $SourceObjects.
$SourceHashFiles = $SourceObjects | Get-FileHash$SourceHashFiles $SourceHashFiles Algorithm Hash Path --------- ---- ---- SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Source\Duplicate.txt SHA256 CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB C:\utility\Source\Hash.txt SHA256 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08 C:\utility\Source\Test.txt SHA256 E0C9CC517FFC572206816F6C6D2E3733E7952AFDDBA125D6B059E3228CB306D4 C:\utility\Source\UserCheck.exe SHA256 2F0275B809B545E4CFFD2ED7F5F39E7852018A0C35E13B28D4883EE38E923D89 C:\utility\Source\UserChoice.exe SHA256 2F8FCA808B67E2B78E55F94087F2A0CE50CD5C395110227C07A554AFE6ABC1E0 C:\utility\Source\UserNote.exe SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Source\SubFolder\Duplicate.txt
Now the idea is, I would want to iterate through my $ObjectResults array and for each file that is present in Source and Target (where SideIndicator equals “==”) I would then find the Destination Path, Hash that value and compare Hash values to see if the file is the same. Here is the problem though, the $DestinationObject has a different path and the hashed values might have the same values for multiple files in different locations. As I am iterating through my $ObjectResults, I don’t know how to reference my DestinationObject hashes as I am looping through SourceObject Hashes.
$SourceHashFiles Algorithm Hash Path --------- ---- ---- SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Source\Duplicate.txt SHA256 CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB C:\utility\Source\Hash.txt SHA256 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08 C:\utility\Source\Test.txt SHA256 E0C9CC517FFC572206816F6C6D2E3733E7952AFDDBA125D6B059E3228CB306D4 C:\utility\Source\UserCheck.exe SHA256 2F0275B809B545E4CFFD2ED7F5F39E7852018A0C35E13B28D4883EE38E923D89 C:\utility\Source\UserChoice.exe SHA256 2F8FCA808B67E2B78E55F94087F2A0CE50CD5C395110227C07A554AFE6ABC1E0 C:\utility\Source\UserNote.exe SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Source\SubFolder\Duplicate.txt $TargetHashFiles Algorithm Hash Path --------- ---- ---- SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Destination\Duplicate.txt SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Destination\Hash.txt SHA256 E0C9CC517FFC572206816F6C6D2E3733E7952AFDDBA125D6B059E3228CB306D4 C:\utility\Destination\UserCheck.exe SHA256 2F0275B809B545E4CFFD2ED7F5F39E7852018A0C35E13B28D4883EE38E923D89 C:\utility\Destination\UserChoice.exe SHA256 2F8FCA808B67E2B78E55F94087F2A0CE50CD5C395110227C07A554AFE6ABC1E0 C:\utility\Destination\UserNote.exe SHA256 E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 C:\utility\Destination\SubFolder\Duplicate.txt
As you can see above, the ‘Duplicate.txt’ file is the same file, so there could be a hash value that is the same, so I can’t use that as a unique identifier.
When this script is in production, the source or target may be a mapped drive, so the target path will be a UNC path that looks something like \w16-Server\c$\Path\ and the Source may also be a server or even a local path c:\path\to\different\directory.
So I feel I have the cmdlet constructs I need, but I am not sure how to implement the logic to acheive this.