Using powershell, i have this code block
ForEach ($Filepath in $BaselineFile_NME,$DailyOutputFile_NME)
{
$SHA1 = [Security.Cryptography.HashAlgorithm]::Create( "SHA1" )
$stream_HSH = ([IO.StreamReader]"$Filepath").BaseStream
-join ($SHA1.ComputeHash($stream_HSH) |
ForEach { "{0:x2}" -f $_ })
$stream_HSH.Close()
}
I am receiving this error
Exception calling "ComputeHash" with "1" argument(s): "Cannot access a closed file."
on this line $stream_HSH = ([IO.StreamReader]"$Filepath").BaseStream
It appears that the McAfee virus inspector has locking this file.
Is there a way to do a little code block to retry after a minute or two?
I don't see the point on using a
StreamReaderthere, useFile.Openinstead withReadWriteshare permissions. That might work if your AV has a handle on those files. Adding also theretryfunction where you can define how long you want to wait before retrying.