Powershell Detection Method Script in SCCM
Powershell methods, is there anything they can’t do? No, not really!
In this blog post I will explain how you can use Powershell Detection Methods in SCCM.
As with many other cases with Powershell, the options are endless.
I came across a scenario at a customer where during an application installation, configuration files were to be replaced in the installation directory using a script. Since a configuration line was different in the different files, this had to be checked during the application detection after the installation.
The easy way out would have been to detect the existence of the configuration file, but I also wanted to make sure the content in the file was correct.
When configuring Detection Methods, you want the Detection Method to be as granular as possible.
Some out-of-the-box Detection Methods which work very well:
- Registry keys
- File version
Detection Methods are one of the main culprits when dealing with application deployment issues.
Sometimes the Detection Method is simply misconfigured and the SCCM client does not detect the application as installed, or a Detection Method is configured as non-granular.
An example of this is adding to a script that a text file is to be created and then use this text file for detecting if the application is installed. This is NOT a good way of configuring Detection Methods.
Read more about Powershell Detection Methods here: Microsoft Docs – Introduction to Application Management
POWERSHELL DETECTION METHODS
HOW THEY WORK
When creating Powershell Detection Methods in SCCM, you need to understatnd how they work. This includes understanding how the output of your Powershell Detection Method is intepreted by SCCM.
The below table from Microsoft Docs shows how script output defines the result:
|Script exit code||Data read from STDOUT||Data read from STDERR||Script result||Application detection state|
|0||Not empty||Not empty||Success||Installed|
|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 solution uses Powershell to parse a text file, which in my example is a configuration file. It will then return a result if the string is found.
You can use this solution as an example if you want to look for other strings in text files.
STEP 1 – CREATE APPLICATION
Start by going to the Software Library and press Create Application.
Select Manually specify the application information.
Enter the information of the application.
Input any additional information about the application.
STEP 2 – CREATE DEPLOYMENT TYPE
On the next step, press Add to add a new Deployment Type.
Select Manually specify the deployment type information.
Enter the Nameof the Deployment Type. This can be anything.
Enter the installation instructions of the application according to your requirements.
STEP 3 – CREATE DETECTION METHOD
Now we are the main subject of this blog post, namely the Powershell Detection Method.
Select Use a custom script to detect the presence of the deployment type. Press Edit.
MODIFY THE SCRIPT
Modify the below script to suit your needs. The example below will look strings in 3 different files. You will need to make the following modifications:
- String variables
- String<number>Location variables
Change Script type to Powershell and enter the code below this screenshot in the text box. You will of course need to modify it to suit your needs
STEP 4 – FINALIZE APPLICATION
Once you have added the Detection Method, you will need to go through the rest of the wizard to finalize the prcess.
STEP 5 – DISTRIBUTE & DEPLOY
End by Distributing and Deploying the application to users or devices, depending on your environment.
Powershell Detection Methods are powerful to use when you cannot use the default MSI, registry or file version detections. They are however a bit more complex and getting your head around how the return logic of the script work might take some time.
For all scenarios when you can use a built-in Detection Method, use that, but in all other custom cases you can use Powershell Detection Methods.
- Microsoft Docs – Introduction to Application Management
- Microsoft Docs – How to create Applications in Configuration Manager
- Find the SCCM Powershell module
- SCCM Patch Maintenance Window based on Patch Tuesday
- Create SCCM ADR with multiple deployments
About the author
Daniel Classon works as a Senior Consultant at Mansoft, focusing on Microsoft Configuration Manager, Windows 10 and Powershell