Script in power shell to add checksum as alternate data stream fails with some file names but otherwise works

184 views Asked by At

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}}```
1

There are 1 answers

1
Santiago Squarzon On

Your code can be reduced to this:

Get-ChildItem *.xl* -Recurse | ForEach-Object {
    $lastWriteTime = $_.LastWriteTime
    $_ | Add-Content -Stream MD5 -Value ($_ | Get-FileHash -a md5).Hash
    $_.LastWriteTime = $lastWriteTime
}

Get-ChildItem with the -Filter you have in place will return FileInfo objects, which have a settable LastWriteTime property, there is no reason for using Get-ItemProperty nor Set-ItemProperty over 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 -Path parameter (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.