How to: Add a “Delete Deployment” Action to Right-Click Actions in ConfigMgr 2012

Many times I find myself looking at Monitoring->Deployments in the ConfigMgr 2012 admin console, and find deployments that should be removed. The “out of the box” process to remove deployments is to browse to the object being deployed (Package, Application, Setting, TS, etc.), select the deployment, and then delete. This process requires a few more clicks than I would like to do, so I created a right-click tool that will allow you to delete the deployment from the Monitoring tab.

Download R-Click_DeleteDeployment.zip

deletedeployment
You can download the .zip file and follow the directions in the included ‘info.txt’ file for a fast deployment to your admin console. You can view additional details below.

First you must create the proper r-click context menus. Copy the following .xml, save it as “DeleteDeployment.xml”, and copy it to the following locations (create subfolders as necessary):

  • C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\XmlStorage\Extensions\Actions\e6a632fa-a43f-432f-b081-8fb0b7065f37\DeleteDeployment.xml
  • C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\XmlStorage\Extensions\Actions\d1621955-48ad-4bba-9c85-95f74c0c6538\DeleteDeployment.xml
  • C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\XmlStorage\Extensions\Actions\93218e21-485a-4e2b-9f23-77c76145e214\DeleteDeployment.xml

(Adjust the path as appropriate to your ConfigMgr Admin Console installation path).

<ActionDescription DisplayName="Delete Deployment" MnemonicDisplayName="Delete Deployment" Description = "Deletes Deployment" RibbonDisplayType="TextAndSmallImage"></pre>
<ShowOn>
 <string>ContextMenu</string>
 <string>DefaultHomeTab</string>
 </ShowOn>
 <Executable>
 <FilePath>PowerShell.exe</FilePath>
 <Parameters>-ExecutionPolicy RemoteSigned -File C:\PowerShell\CM12RClickTools\DeleteDeployment.ps1 "##SUB:DeploymentID##" "##SUB:__Server##" "##SUB:__Namespace##" "##SUB:CollectionID##" </Parameters>
 </Executable>
 </ActionDescription>

Notice in the .xml that we specify the -ExeuctionPolicy RemoteSigned parameter. Best practice would be to sign your scripts, but if that’s not something you can manage easily, you can use the parameter as shown to modify the execution policy for the specific instance you’re launching, instead of changing the execution policy for all script execution. Next, copy the PowerShell code below, and save it to C:\PowerShell\CM12RClickTools\DeleteDeployment.ps1


#use this script to delete Deployments from the monitoring node.
#capturing arguments:
$DeploymentID = $args[0]
$SiteServer = $args[1]
$SiteNamespace = $args[2]
$SiteCode = ($args[2]).Substring(14)
$CollectionID = $args[3]
$SiteDrive = $sitecode + ":"
"Deleting Deployment..."

#Connect to PowerShell Provider:
$ModulePath = (($env:SMS_ADMIN_UI_PATH).Substring(0,$env:SMS_ADMIN_UI_PATH.Length-5)) + '\ConfigurationManager.psd1'

import-module $modulePath -force
if ((get-psdrive $siteCode -erroraction SilentlyContinue | measure).Count -ne 1) {
new-psdrive -Name $siteCode -PSProvider "AdminUI.PS.Provider\CMSite" -Root $SiteServer
}
cd $siteDrive

"Get Deployment Info..."
$Deploy = get-cmdeployment -deploymentid $DeploymentID
$Deploy

"Removing Deployment..."
Switch ($deploy.FeatureType) {
1 {
#Application
remove-cmdeployment -ApplicationName $Deploy.SoftwareName -DeploymentID $Deploy.DeploymentID -force
}
2 {
#Package/Program
#Set Filter
$advertFilter = "AdvertisementID='$DeploymentID'"
#query WMI and Delete
gwmi sms_advertisement -Namespace $SiteNamespace -ComputerName $SiteServer -filter $advertFilter | % {$_.Delete()}
}
5 {
#software update
#Set Filter
$advertFilter = "AssignmentUniqueID='$DeploymentID'"
#query WMI and Delete
gwmi SMS_UpdatesAssignment -Namespace $SiteNamespace -ComputerName $SiteServer -filter $advertFilter | % {$_.Delete()}
}

6 {
#baseline
#Set Filter
$advertFilter = "AssignmentUniqueID='$DeploymentID'"
#query WMI and Delete
gwmi sms_baselineassignment -Namespace $SiteNamespace -ComputerName $SiteServer -filter $advertFilter | % {$_.Delete()}
}
7 {
#Task Sequence
#Set Filter
$advertFilter = "AdvertisementID='$DeploymentID'"
#query WMI and Delete
gwmi sms_advertisement -Namespace $SiteNamespace -ComputerName $SiteServer -filter $advertFilter | % {$_.Delete()}
}

Default {
#display feature type
write-host ("No defined method to delete " + $deploy.FeatureType)
}

}

Write-Host "Complete. Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Now re-launch your ConfigMgr Admin console to use the new r-click->Delete Deployment feature. As you can see from the code, we have to take different actions based on the object type to delete. Remove-CMDeployment only removes Application deployments, while Get-CMDeployment returns all deployments.
We use Get-CMDeployment to obtain vital information for each deployment, and then based on featuretype, perform the appropriate Delete method.

Happy Scripting!

Greg
ramseyg@hotmail.com

About Greg Ramsey
Greg Ramsey is a systems engineer specializing in global systems management for Dell IT. He has a B.S. in Computer Sciences and Engineering from the Ohio State University and is a Microsoft Most Valuable Professional (MVP) for Microsoft System Center Configuration Manager. Greg coauthored SMS 2003 Recipes: A Problem-Solution Approach (Apress, 2006), Microsoft System Center Configuration Manager Unleashed (Sams, 2009), Microsoft System Center 2012 Configuration Manager Unleashed (Sams, 2012), and Microsoft System Center 2012 Configuration Manager: Administration Cookbook (Packt Publishing, 2012). Greg is cofounder of the Ohio SMS Users Group, and the Central Texas Systems Management User Group.

3 Responses to How to: Add a “Delete Deployment” Action to Right-Click Actions in ConfigMgr 2012

  1. John says:

    Any chance we could have the code from UD-B406 for the App Approvals? I’m very interested in it after watching the webcast of the session.

    • Greg Ramsey says:

      Yes, I plan to release all code from the session over the next two weeks. App approval will probably be last, as it’s a little more complex and I need to ensure code is clean. :)

  2. Pingback: How to: Add a “Delete Deployment” Action to Right-Click Actions in ConfigMgr 2012 « Chris Nackers Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,246 other followers

%d bloggers like this: