Saturday, August 13, 2016

SCCM Powershell - Application Content Maintenance


So last post I showed a quick SQL query to identify all stagnant Applications at a client site.  Now we can't just stop there, right?  Of course not.  We have to go through the next process of removing the content of those stagnant Applications from the Distribution Points, this is where automation is welcomed.


We took the results of the query from an earlier post (http://www.pushdeploy.com/2016/08/sccm-application-content-maintenance.html) and placed them in a CSV and removed all columns except for the "DsiplayName", we then renamed its header to simply "Applications".  The goal is to pipe this into a Powershell command to automate the removal of the Application content from each Distribution Point.  Luckily for us there is just a command: Remove-CMContentDistribution

With this command in hand we ran the following into a Powershell prompt as a test to see the results with one application on one Distribution Point. (obviously the DP Name is edited out to protect the innocent).


------------------------------------------------------------

remove-cmcontentdistribution -applicationname "Adobe Flash 22.0.0.192" -DistributionPointName Server1.Testlab.Local -verbose

------------------------------------------------------------

 Below is what we received.



And this is why we test first.  The verbose tag in the command helped us see the more detailed view of what is occurring as this is running.  In the end we see the error "No content destination was found.  This can happen when an invalid collection, distribution point, or distribution point is specified."  Hmm, when an "invalid distribution point, or distribution point is specified".  Anyway, so we saw an available option for "-DistributionPointGroupName" and made a fair assumption that might be required as well.  So we created a DP Group in SCCM that included all the DP's in the environment and simply called it "All".  We reran the command now with the extra option for the DP Group Name "All" and now we had success where the Adobe Flash application content was successfully removed from the distribution point.

The client had close to 100 Application content that had to be removed so doing this one at a time is not the best method.  Now I don't claim to be a Powershell expert so I'm sure someone could do this script much better but below are my results.  (I tried putting all DPs in a variable but the command seems to only like one DP at a time specified, hence multiple lines of same command but with just a different DP name).


------------------------------------------------------------
$csv = import-csv C:\Temp\Cleanup\Apps.csv
foreach ($App in $csv.applications) {
     Remove-CMContentDistribution -ApplicationName $APP -DistributionPointName Server1.testlab.local -DistributionPointGroupName All -verbose -force
     Remove-CMContentDistribution -ApplicationName $APP -DistributionPointName Server2.testlab.local -DistributionPointGroupName All -verbose -force
     Remove-CMContentDistribution -ApplicationName $APP -DistributionPointName Server3.testlab.local -DistributionPointGroupName All -verbose -force
     Remove-CMContentDistribution -ApplicationName $APP -DistributionPointName Server4.testlab.local -DistributionPointGroupName All -verbose -force
------------------------------------------------------------

So to walk through this, the first line imports the CSV we created earlier.  We then have it run through each cell under the "App" header and run the command "Remove-CMContentDistribution" with our required parameters.  The output of a success for this command is below:


And if it fails to find the content on the Distribution Point (because we just did a blanket All DPs) you will get the below result which as you can see is the same result that we received when our command was incorrect:



Now some of you might be asking why we didn't just delete the Application and call it a day.  The client saw the need to keep the Applications to reference back to, and I saw no harm in keeping the Application around as long as it wasn't hogging up space on various DPs.

No comments:

Post a Comment