ConfigMgr: -RemoveDetectionClause with Set-CM(Script/Msi)DeploymentType

Note April 3, 2019: CM1902 has been released with a fix to properly handle version numbers for detection clauses.

Note December 7, 2018:  Using New-CMDetectionClauseWindowsInstaller with ExpectedValue (ProductVersion) seems to work, but the final xml data shows the version number is a string instead. For .msi detections, I end up having to go in and manually browse for the file and set up the detection that way. Hoping it’ll be fixed in CM1902.

I began writing PowerShell scripts to help automate updating/replacing applications in ConfigMgr. About half of the programs are .exe with PowerShell detection and the other half are .msi using windows installer detection. Microsoft’s current documentation for removing detection clauses just has [-RemoveDetectionClause <String[]>] without any extra information.

Testing the command (Set-CMScriptDeploymentType -ApplicationName “appname” -DeploymentTypeName “apptitle” -RemoveDetectionClause “blahblah”) returns a warning message: “WARNING: Detection clause with a logical name of ‘blahblah’ was not found and will be ignored for removal.” I then looked into the deployment types SDMPackageXML and found two different id types. One called ‘LogicalName’ and the other called ‘SettingLogicalName’. Manually tested both entries and the SettingLogicalName id worked.

# Get application and deployment type title
$AppName = "Google Chrome"
$AppTitles = (Get-CMApplication -ApplicationName "$($AppName)" | ConvertTo-CMApplication).DeploymentTypes.Title
ForEach ($Title in $AppTitles) {
   $SDMPackageXML = (Get-CMDeploymentType -ApplicationName "$($AppName)" -DeploymentTypeName "$($Title)").SDMPackageXML

   # Regex to retrieve all SettingLogicalName ids. Will be duplicates for every one but doesn't seem to be an issue.
   [string[]]$OldDetections = (([regex]'(?<=SettingLogicalName=.)([^"]|\\")*').Matches($SDMPackageXML)).Value

   # Create your new detection clause(s) and remove all of the previous ones
   $MsiClause = New-CMDetectionClauseWindowsInstaller -ProductCode "<MSIGUID>" -ExpectedValue "<MSIVERSION>" -ExpressionOperator GreaterEquals -Value
   Set-CMScriptDeploymentType -ApplicationName "$($AppName)" -DeploymentTypeName "$($Title)" -AddDetectionClause ($MsiClause) -RemoveDetectionClause $OldDetections

This is a basic example of getting all current detection clauses of a deployment type and removing them with a newly created clause. (Getting an msi’s guid & version separately)