SCCM Configmgr 2012 Powershell script cleanup duplicate obsolete and outdated computers Via Task Scheduler

This post is continuation to my previous post on how to clean outdated computers from configmgr Database. More info refer , http://eskonr.com/2014/03/sccm-configmgr-2012-delete-duplicate-obsolete-records-etc-using-collections/

Through this post,i will show you to create Powershell script cleanup duplicate obsolete and outdated computers Via Task schedule that deletes the computers from specific collections to maintain configmgr database healthy.

In my previous blog,we have seen how to create collections for duplicate,obsolete,computers not contacted to domain during X days with hardware inventory older than 30 days etc.

Note: if you are imaging the computers frequently ,you may see many duplicate/obsolete computers are you are required to cleanup those entries at regular intervals.

Creating collections is nice but how frequent do you clean these entries ? keep checking once in week or once in month to delete the entries from database to have healthy environment and high success ratio on client health when your boss ask for client health statistics?

There are configmgr Maintenance tasks that does the cleanup job for you like delete inactive,aged inventory data etc based on what task you enable. More about Maintenance tasks,refer http://eskonr.com/2012/01/configmgr-sccm-2012-site-maintenance-tasks/ and default settings for maintenance tasks http://support.microsoft.com/kb/2897050

Through this post,i will show how to create powershell script to do the removal of computer records from specific collections (you can have many collections that contains out dated computers with different criteria) and empty the collection on scheduled basis.

 

I have captured few lines of code from David blog http://www.david-obrien.net/ to get site code,site server name etc instead of providing them manually.

Download the script from TechNet Gallery http://gallery.technet.microsoft.com/SCCM-Configmgr-2012-1446ec07

Script function: It takes the collectionIDs as input parameter what you supply while running the script,check if the collection Exist ,check if the collection is not empty  and then perform removal of computer Objects.

Before removal of computer objects ,script will write computer objects to txt file for reference incase you need check later.

After you download the script,put it in either share folder or on local server.

PS:You don't have to make any changes to the script ,just get the collection's which you want to cleanup.

Create Task scheduler with recurring weekly or monthly at your convenience,get help from http://windows.microsoft.com/en-sg/windows/schedule-task#1TC=windows-7 .

After you create Task scheduler,open the properties of Task and go to Action Tab,select the Actions ,click on Edit.

image

For program,/Script: type PowerShell.exe ,and for Arguments (Optional): type -command "& 'G:\SCCM Tools\delete-resources-from-collection.ps1' 'PRI0001E' ,'PRI00020'

where 'G:\SCCM Tools\delete-resources-from-collection.ps1: is powershell script what we created earlier and PRI0001E,PRI00020 are the collections that i have to cleanup. If you have more collections ,just append the existing command like with ,’collectionID’.

Actions Tab:

image

Make sure you run the task with right permissions on the database .you can change the account name to run the task from General Tab:

image

run the task,wait until it finishes. Go back your script location,you will see .txt file created with script ran date:

image

The txt file contains list of computers which are removed from the specific collection.

Hope if helps for someone who wants to keep database active without outdated Clients ;).

If you want to know what causes the client become Inactive ,refer http://www.david-obrien.net/2014/04/12/configure-client-activity-settings-configmgr/

7 Responses to "SCCM Configmgr 2012 Powershell script cleanup duplicate obsolete and outdated computers Via Task Scheduler"

  1. Have you ran the script to delete computers from x Collection recently? Trying to figure out why it's not working for me.

    "PS D:\Scripts> .\Delete_Computers_From_Collection.ps1 nt1000a2
    Method invocation failed because [System.Management.Automation.PSInternalMemberSet] does not contain a method named
    'delete'.
    At D:\Scripts\Delete_Computers_From_Collection.ps1:63 char:10
    + $comp.psbase.delete()
    + ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (delete:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound"

    Reply

Leave a Reply to Eswar Koneti Cancel reply