Code Release
HashCompare compares a file with a known MD5 hash and returns the output in an SCCM compatible manner. To understand more about why this script exits as it does, you should read “To use a custom script to determine the presence of a deployment type” (Under step 4) on technet.
Usage
Configuration
The script is fairly simple. You only need to configure the top 2 lines. First set $FilePath to the location of the pre-existing file. Then set $ExpectedHash to the MD5 hash of the deployed file. You can get the MD5 hash from powershell using Get-Hash in Powershell 4.0 or, if you are not up to 4.0, by using this Get-Hash.
General Usage
Inside of a powershell console enter the following:
&""
Example:
&"C:\Scripts\HashDetect.ps1"
SCCM Usage
After configuration, import the script as a new Detection Method for your deployment.
Script
$FilePath = "C:\ProgramData\MyApp\MySettings.xml"
$ExpectedHash = "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
if (-not (Test-Path $FilePath))
{
#File path not found. Not installed.
exit 0
}
try
{
$FileStream = New-Object IO.FileStream $FilePath, "Open"
$MD5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$Hash = [System.BitConverter]::ToString($MD5.ComputeHash($FileStream))
}
catch
{
Write-Error $_.ToString()
exit 1
}
#Compare the hash with a known hash
if ($Hash -eq $ExpectedHash)
{
Write-Output "Hash match. File already installed."
exit 0
}
else
{
#Hash MisMatch. Not installed
exit 0
}