This blog post will describe how you can remove all deployments for an application in ConfigMgr 2012 SP1. Perhaps the most easiest way would be to use the built in cmdlet Remove-CMDeployment, but I thought it would be fun to do it with WMI and see what happens behind the scenes. Doing it with WMI lets you perform the same action that the Remove-CMDeployment cmdlet, but without loading the ConfigMgr PowerShell module.

Overview

  • Let’s take a look at the script
  • Running the script
  • What happened?

Let’s take a look at the script

What is important to know about if you’d like to delete a deployment for an application through WMI, is the SMS_ApplicationAssignment class. In that class you’ll find the WMI objects that you need to remove. The script takes the value in the $ApplicationName variable from the mandatory parameter, and does a Get-WmiObject in order to get the WMI object that needs to be removed. Since there can be multiple deployments for an application, we store the WMI objects __PATH property in a variable called $Deployment. The script then goes through each item in the $Deployment variable and removes the WMI objects if any was found. If no deployments was found, the script will let you know that it didn’t find any deployments for the given application.

Save the below code to C:\Scripts\Remove-ApplicationDeployment.ps1.

#========================================================================
# Created on:   2013-09-13 16:43
# Created by:   Nickolaj Andersen
# Filename:     Remove-ApplicationDeployment.ps1
#========================================================================

param(
[parameter(Mandatory=$true)]
$SiteServer,
[parameter(Mandatory=$true)]
$SiteCode,
[parameter(Mandatory=$true)]
$ApplicationName
)

if (Get-WmiObject -Namespace "root\sms\site_$($SiteCode)" -Class "SMS_Application" -ComputerName $SiteServer -ErrorAction SilentlyContinue | Where-Object { $_.LocalizedDisplayName -like "$($ApplicationName)"}) {
	$Deployment = (Get-WmiObject -Namespace "root\sms\site_$($SiteCode)" -Class "SMS_ApplicationAssignment" -ComputerName $SiteServer | Where-Object { $_.ApplicationName -like "$($ApplicationName)"}).__PATH
	$i = 0
	if (($Deployment -eq $null) -or ($Deployment -eq "")) {
		Write-Warning "No deployments was found for application $($ApplicationName)"
	}
	else {
		$Deployment | ForEach-Object {
		$i++
        Write-Output ""
		Write-Output "Deleting deployment $($i) of $($Deployment.Count): $($_)`n"
		Remove-WmiObject -InputObject $_ | Out-Null
        Write-Output "Successfully deleted $($i) deployments for $($ApplicationName)`n"
		}
	}
}
else {
	Write-Warning "Application '$($ApplicationName)' was not found"
}

Running the script

Let’s say that we’d like to remove all deployments for an application called 7-Zip 9.20 x64.

49_2

1. Open an elevated PowerShell console.
2. Browse to C:\Scripts.
3. Run the following command:

.\Remove-ApplicationDeployment.ps1 -SiteServer CM01 -SiteCode P01 -ApplicationName "7-Zip 9.20 x64"

49_1

If you’ve entered the name of the application correctly, the script will then remove all deployments found for that application. The output will look something like this:

49_3

What happened?

What’s interresting to note from the above screenshot is the AssignmentID number. It will be the last 8 digits from the __PATH property. For the application in my lab environment it was 16777217. If we open the SMSProv.log stored in <ConfigMgr installation path>\Logs with CMTrace.exe, we can see that an event fired off to do a DeleteInstanceAsync for the 16777217 AssignmentID.

49_4

If we now take a look in the ConfigMgr console, the deployment has been removed. Additionally, the DeploymentSummary information have also been removed.

49_5

Nickolaj Andersen
Principal Consultant and Enterprise Mobility MVP. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows deployments and Automation. In 2015 Nickolaj was awarded as PowerShell Hero by the community for his script and tools contributions. Author of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService and a frequent speaker at user groups.

(602)

There are no comments.

Leave a Reply