ConfigMgr Powershell Application Detection Methods

3 minutes read

I am being asked this quite a lot, so I thought I might also write about it a bit.

Already a while back a colleague asked me what needs to be done to make Application model Detection Methods work with Custom Powershell scripts.

Powershell detection Method SCCM

I believed it to be really straight forward, but actually in the beginning it was a bit weird.

Application model Detection Methods

At that time there wasn’t much documentation on that topic around, so we had to test a bit around on how Detection Methods work in general and how they work with custom scripts. Now this is pretty well documented on TechNet (How to Create Applications in Configuration Manager) , but here’s a quick recap.

In general, in order for an Application Deployment to work, you would need to have at least one Deployment Type configured for that Application. Based on certain requirements (configurable) the Application decides which Deployment Type needs to be chosen. Now that Deployment Type also needs to have a Detection Method which evaluates if that Application is already installed or not. If it’s the latter then it has to be installed. The easiest way to do a Detection Method is when you’re deploying an MSI and can use the MSI Product Code to detect the installed application. ConfigMgr will even create that all for you. (which is great for automation by the way! see /2013/07/10/create-new-configmgr-applications-from-script-with-powershell/)

The above TechNet article shows that you can not only use MSI Product Code, but also loads of other ways, like does a certain file exist and does it also have a specific file size.

SCCM Detection Method

If that is not enough you can check for Registry Keys and if even that doesn’t go far enough you can write your own scripts to detect an installed application, and this is where it gets interesting.

Powershell, VBScript and JSharp as CM12 Detection Methods

You can chose between three script languages for your Detection Methods –> Powershell, VBScript and JSharp. I’m only really fluent in Powershell, so that’s the one I chose.

I won’t give you a generic script (which doesn’t exist), only what you need to know when writing such Detection Method Scripts, no matter which language you chose.

What’s important with Detection Methods is that they are used to detect if an Application is already INSTALLED. That is all it really cares about. What does that mean?

if (test-path C:\Apps\Test.html)
   {
        Write-Host "Installed"
   }
else
   {
        Write-Host "Not Installed"
   }

This Detection Method will always tell the “Appdiscovery.log” and the ConfigMgr Agent that this Application is already installed.

if (test-path C:\Apps\Test.html)
   {
        $true
   }
else
   {
        $false
   }

Even this won’t do.

Looking at the TechNet Article from above we see the following table which explains a lot.

Script exit code Data read from STDOUT Data read from STDERR Script result Application detection state
Empty Empty Success Not installed
Empty Not empty Failure Unknown
Not empty Empty Success Installed
Not empty Not empty Success Installed
Non-zero value Empty Empty Failure Unknown
Non-zero value Empty Not empty Failure Unknown
Non-zero value Not empty Empty Failure Unknown
Non-zero value Not empty Not empty Failure Unknown

 

This tells us that instead of one of the above scripts, we need to do this:

if (Test-Path C:\Apps\Test.html)
    {
        Write-Host "Installed"
    }
else
    {
    }

If the Agent can’t find that file in that Path it won’t write anything back to the Console or STDOUT and the agent knows that this Application hasn’t been installed yet.

In the end it’s really not too complicated using Powershell for your Detection Methods and I actually like using it a lot.

Updated:

Leave a Comment