SCCM PowerShell Script Detection Method

Ever wanted to know how to use the script detection method of an application in ConfigMgr with PowerShell, its quite simple really once you have been shown what ConfigMgr expects to be returned.

The detection method bellow is a PowerShell Test-Path statement. If the statement returns “True”, meaning the file is there, then the script shouts out to the ConfigMgr client to say the detection method is satisfied. You MUST keep the Else clause in the script empty or it will fail to evaluate although there is nothing to be run in it.

if( Test-Path "$env:LOCALAPPDATA\Microsoft\Onedrive\OneDrive.exe" )

{
Write-Host "installed"
}

else
{
}

Now as i’m sure you’ve guessed with the example this becomes really usefull when deploying applications that install in the users %LocalAPPDATA% as ConfigMgr currently cannot query that location as all installs run as system.

Here is another testing two paths.

if( ( Test-Path "$env:LOCALAPPDATA\Microsoft\Onedrive\OneDrive.exe" ) -and ( test-path "HKCU:\SOFTWARE\Microsoft\OneDrive\17.3.6390.0509" ) )

{
Write-Host "installed"
}

else
{
}

These examples should let you build all sorts of detection methods now. It doesn’t have to be test path either, it could be anything like checking if a registry entry value is “greater than or equal to”.

 

4 Replies to “SCCM PowerShell Script Detection Method”

  1. Just found this. Thanks and wanted to inquire about something I’m trying to do. The application I’ve created in SCCM runs a powershell script that copies all files from a ‘media’ folder in the application package to c:\media. For my detection script, I want to compare the list of files in the application package media folder to the files the c:\media folder. I presume the package is downloaded to a folder in c:\windows\ccmcache before the detection script is run to determine if the app needs to be installed or not. Is there a way I can reference whatever that c:\windows\ccmcache\xx location may be within the powershell script? An environment variable or something? I tried this:
    $scriptdir = Split-Path $script:MyInvocation.MyCommand.Path
    But when I check the appdiscovery log file, it’s invocating the detection script from C:\Windows\CCM\SystemTemp…not from the application cache directory.

    1. Hey,
      Firstly apologies for the late reply, detection scripts do indeed run from the SystemTemp folder in the CCM Client directory.
      To carry out what you are doing, I would suggest either Get-Childitem to search for you script folder in the c:\windows\ccmcache\* location and then write logic to compare the files once found against your c:\media folder.
      Or… Compare the files directly after the copy logic in your install script and then tag the registry HKLM:\Software\YourCompanyName with a string with the value being the date(version) of the script. Then just check for the reg entry for detection.
      Cheers,
      SCCMOG

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright 2016 SCCMOG | All Rights Reserved

css.php