I want to collect the hash of a set of files and store them in a baseline. I later want to collect the hash of the same files and store them in a new file, i.e. Difference.xml. I then want to compare the hash of these two files, and if there is a change, list each line item file that has changed along with the hash, the file path, and the last modified date. Just to be clear, I am only comparing the hash of the files containing multiple hashes, and only doing a line by line comparison if there is a difference. I would like to output this to Xml.
Here is what I have come up with:
$Hashstore='d:\Baseline.xml'
$HashCompare='d:\Difference.xml'
$output="d:\Results_$DateTime.xml"
$files=@('d:\scripts\*','d:\FileCheckerv2.ps1','d:\Change Request.csv')
if(test-path$hashstore){
}
else {
Get-FileHash$files-Algorithm MD5 | Export-Clixml$Hashstore
}
$current=Get-FileHash$files-Algorithm MD5 | Export-Clixml$HashCompare
if($HashStore=$HashCompare) {
Write-Host'Files are the same'-BackgroundColor green
}else{
Write-Host'Differences Found!'-ForegroundColor red
# compare
$results=Compare-Object$baseline$HashCompare
$results | Export-CliXml$output
Get-FileHash$files-Algorithm MD5 | Export-Clixml$Hashstore
}
This is reaaally close, but for some reason, if I go and change the contents of one of the files, the difference object will reflect the new hash, but will still give me the true of the if statement. Also, when I run
It seems like it tries to compare the contents of the file that is $baseline with the file that is $hashcompare, a la
Compare-Object $baseline $HashCompare
PS D:\> Compare-Object $baseline $HashCompare | Write-Host
@{InputObject=d:\Difference.xml; SideIndicator==>}
@{InputObject=; SideIndicator=<=}
@{InputObject=; SideIndicator=<=}
@{InputObject=; SideIndicator=<=}
@{InputObject=; SideIndicator=<=}
What do I need to do to get the script to insert the last modified date, and make the comparisons correctly?
Thanks in advance!