I want to check files for integrity with a checksum. To make it easier I put the hash into an alternate data stream of the file. When someone alters the file I can verify this with the checksum.
However, when I add a data stream the file's LastWriteTime gets updated, so I added functionality to reverse it.
It works like a charm - mostly. But it fails with some files, about 5%. I have no idea why. It looks like it fails with file names that contain spaces or extra dots, but many other that have spaces and multiple dots in the file name work just fine.
Does anyone know what's going on, how to prevent these failures or how to improve the code? Thanks!
The code:
$filenames = Get-ChildItem *.xl* -Recurse | % { $_.FullName }
foreach( $filename in $filenames ) { ForEach-Object { $timelwt = Get-ItemProperty $filename | select -expand LastWriteTime | select -expand ticks } {add-content -stream MD5 -value (Get-FileHash -a md5 $filename).hash $filename } { Set-ItemProperty $filename -Name LastWriteTime -Value $timelwt}}```
Your code can be reduced to this:
Get-ChildItemwith the-Filteryou have in place will returnFileInfoobjects, which have a settableLastWriteTimeproperty, there is no reason for usingGet-ItemPropertynorSet-ItemPropertyover them.As for, why your code could be failing, the likeable explanation is that you have some file paths with wildcard metacharacters, and since you're not using
-LiteralPath, the cmdlets are defaulting to the-Pathparameter (which allows wildcard metacharacters).As aside, I would personally recommend you to create a separate checksum file for the files instead of adding an alternative data stream.