HashCompare

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
}

Installing a Custom Service

This post was migrated from my older blog.

So I was playing around with custom services in C# a couple years back. I never created a service before so I was going off of Microsoft’s MSDN articles and a few blogs that Google searches turned up. They all followed the same pattern.

This solution works fine, but I don’t really like it much.

Fast forward to about a month ago. We had some issues with one of our servers. One of the steps in troubleshooting the issue was to manually check what the machine and resources looked like from Local System’s perspective. To do this, I opened an instance of cmd.exe under the Local System account interactively using the built-in tool SC. (More on how to do that here) I did not know about that tool until then. Turns out, all it does is register the application specified, cmd.exe in this case, as a service. I decided to test it for use in installing a custom service created in C#. I created a quick service that answered a single incoming TCP connection with “Hello World”. Installed it with SC, and it worked! I did not have to create an install class within the service, nor did it rely on InstallUtil.exe. For more information on SC for installing services, check out the “SC Create” TechNet article