Introduction

Powershell methods, is there anything they can’t do? No, not really!
In this blog post, I explain how you can use Powershell Detection Methods in SCCM to detect file content.

As with many other cases with Powershell, the options are endless.

In this blog post, I will explain how to create a detection method in SCCM, based on a Powershell script. The detection method will detect if a specific content is present in a text file.

My detection method scenario

I came across a scenario at a customer where during 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.

Detection methods

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:

  • MSI
  • Registry keys
  • File version

Detection Methods are one of the main culprits when dealing with application deployment issues.

Sometimes the Detection Method is 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 the right way of configuring Detection Methods.

Read more about Powershell Detection Methods here: Microsoft Docs – Introduction to Application Management

Powershell detection methods

How Powershell detection methods work

When creating Powershell detection methods in SCCM, you need to understand how they work. Understanding how Powershell detection method works include understanding how SCCM interprets the output of your Powershell detection method.

The below table from Microsoft Docs shows how the script output defines the result:

Script exit codeData read from STDOUTData read from STDERRScript resultApplication detection state
0EmptyEmptySuccessNot installed
0EmptyNot emptyFailureUnknown
0Not emptyEmptySuccessInstalled
0Not emptyNot emptySuccessInstalled
Non-zero valueEmptyEmptyFailureUnknown
Non-zero valueEmptyNot emptyFailureUnknown
Non-zero valueNot emptyEmptyFailureUnknown
Non-zero valueNot emptyNot emptyFailureUnknown

My Powershell detection method solution

Introduction

This solution uses Powershell to parse a text file, which in my example is a configuration file. The Powershell script returns a result upon string discovery.

You can use this solution as an example if you want to look for other strings in text files.

How to implement the Powershell detection method solution

Step 1 – Create the application

Start by going to the Software Library and press Create Application.

Select Manually specify the application information.

Enter the information about the application.

Input any additional information about the application.

Step 2 – Create the deployment type

On the next step, press Add to add a new Deployment Type.

Select Manually specify the deployment type information.

Enter the Name of the Deployment Type. The Deployment Type name can be anything.

Powershell detection methods

Enter the installation instructions of the application according to your requirements.

Powershell detection methods

Step 3 – Create the Powershell 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.

Powershell detection methods
Modify the Powershell 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

Powershell detection methods
Powershell detection method example
#Define strings and their location. Also include the filename.

$String1 = ""
$String1Location = ""

$String2 = ""
$String2Location = ""

$String3 = ""
$String3Location = ""

# Detect presence of String1 in String1 Location
try {
    $String1Exists = Get-Content $String1Location -ErrorAction Stop
}
catch {
}

# Detect presence of String2 in String2 Location
try {
    $String2Exists = Get-Content $String2Location -ErrorAction Stop
}
catch {
}

# Detect presence of String3 in String3 Location
try {
    $String3Exists = Get-Content $String3Location -ErrorAction Stop
}
catch {
}

if (($String1Exists -match $String1) -and ($String2Exists -match $String2) -and ($String3Exists -match $String3)) {
    Write-Host "Installed"
}
else {
}

Step 4 – Finalize the application

Once you have added the Detection Method, you will need to go through the rest of the wizard to finalize the process.

Step 5 – Distribute and deploy the application

End by Distributing and Deploying the application to users or devices, depending on your environment.

Conclusion

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.

References

Related posts

2 COMMENTS

  1. Hi Daniel
    Can we have the full detection script somewhere? The bottom few lines are missing from the screenshot
    Regards
    Andy

LEAVE A REPLY

Please enter your comment!
Please enter your name here