Monday, March 18, 2013

CM 2012 Application Model - Custom Detection Methods

The new Application Model in CM 2012 includes detection methods. These methods must be met for an application to be labeled as "Installed". This same process prevents an application from being installed if the method is already met.

The default detection methods work great for standard applications. Every once in a while though, you may need to use a custom method. Custom methods are scripts that can be ran to determine if an application is installed. I use the term "installed" loosely, simply meaning that the objective of the application has been reached. You may have applications that change a setting, and do not install anything. These custom methods may be the only way to determine if that setting has been changed.

There are three scripting types that you can choose from - PowerShell, VBScript, and JScript. I do everything possible in PowerShell, so that will be the focus of this blog. You can reference this TechNet article for detail on what I am going to show: http://technet.microsoft.com/en-us/library/gg682174.aspx. I cannot link straight to the section, so expand "Step 4: Configure Detection Methods to Indicate the Presence of the Application" and "To use a custom script to determine the presence of a deployment type". This shows the chart that I will reference.

So as you can see with this chart, there are several combinations of exit codes and strings that can be used to indicate whether an application is installed. I use an exit code of zero and a STDOUT string of "The application is installed" to indicate that the application is already installed. STDOUT can be set to anything, as long as it is not blank. I use an exit code of zero and a STDOUT of empty to indicate that the application is NOT installed.

To test if an application is present, I simply do an IF statement that sets the correct exit code and string. Here is an example. This example is testing if the SCCM client cache is greater than 25600MB. If it is, the application is considered installed. If it is not, the application is considered not installed.

If (((Get-WmiObject -namespace root\ccm\SoftMgmtAgent -class CacheConfig).Size) -ge "25600") {
Write-Host "The application is installed"
Exit 0 }


If (((Get-WmiObject -namespace root\ccm\SoftMgmtAgent -class CacheConfig).Size) -lt "25600")  {
Exit 0 }


Disclaimer: I know that this can be done with an ELSE instead of two IF statements, but this method helps me to see what is going on clearer.

To write the STDOUT string, you simply need the Write-Host command, which the first part of the script is doing.

It is very important to set the exit code. As you can see in the chart, if the exit code is not set, the application detection state is set to "Unknown", meaning the CM doesn't know what to do with it. The application will neither install, or show up as installed.

Bottom line, you can use anything that can be scripted to detect if an application is present. At the end of the script, simply set the exit code to 0 and write the STDOUT string if the application is present, or leave it empty if the application is not present.

I hope this helps in your application deployments.

1 comment:

  1. Thanks for this info, it helped me setup a detection method with powershell. i was initially rather confused about how to set STDOUT

    ReplyDelete