Removing Duplicate Device Objects from Configuration Manager with PowerShell

Published Apr 25 2022 04:25 PM 2,260 Views
Microsoft

Hello everyone, Chris Vetter Customer Engineer at Microsoft back again to discuss an all-too-common problem and that is Duplicate Device Hostnames in Microsoft Endpoint Manager Configuration Manager (MEMCM). This commonly tends to happen when performing an OSD Image on a known device (known meaning the device already exists inside your database) without deleting the object out of Active Directory. What we get is duplicate device objects which can cause conflicts when updating policy or collecting inventory and the device record you want updated is not because the action was performed on the duplicate that is not actually associated with the client.

There is no automatic or supported way to detect and remove these duplicate objects but there is a workaround that you can use and even automate depending on how large of an issue this is in your environment. We are going to create a Device Collection of the duplicate device hostnames then use a PowerShell script to remove all the objects from the collection (and the Database) this way the correct object can be added back through discovery.

 

Step 1: Create Device Collection of Duplicate Hostnames

I am going to assume you know how to create a device collection and if you do not there are many great articles and documents on the web that can show you how, so in this step I will provide the WQL query you can use to populate the collection with the duplicate hostnames.

 

Select R.ResourceID,R.ResourceType,R.Name,R.SMSUniqueIdentifier,R.ResourceDomainORWorkgroup,R.Client from SMS_R_System as r full join SMS_R_System as s1 on s1.ResourceId = r.ResourceId full join SMS_R_System as s2 on s2.Name = s1.Name where s1.Name = s2.Name and s1.ResourceId != s2.ResourceId and R.Client is null

 

Step 2: Run the PowerShell to remove the objects

This is an easy one liner you can run after you connect to the PS Drive of your MEMCM Site which you should be able to do from any machine where you have the MEMCM Admin Console installed.

Get-CMCollectionMember -CollectionName "<Collection Name Here>" | Remove-CMDevice -Force

 

Depending on how many objects are on your collection will determine how long it will take for the script to complete.

 

Step 3: Automate

If this is a continuous problem in your environment, I would recommend running this script on a schedule. You can download the complete script here that will connect to the MEMCM PS Drive and run the PowerShell line to remove the objects. How often you need to run it would depend on how quickly the collection you created repopulates. We really should get in the practice of tuning our discovery methods and removing the device from AD to minimize this issue.

 

Disclaimer

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

All processes and directions are of my own opinion and not of Microsoft and are from my years of experience with the configuration manager product in multiple customer environments.

 

Resources

Get-CMCollectionMember (ConfigurationManager) - Configuration Manager | Microsoft Docs

Create queries - Configuration Manager | Microsoft Docs

2 Comments
%3CLINGO-SUB%20id%3D%22lingo-sub-3370632%22%20slang%3D%22en-US%22%3ERe%3A%20Removing%20Duplicate%20Device%20Objects%20from%20Configuration%20Manager%20with%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3370632%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F242924%22%20target%3D%22_blank%22%3E%40Chris%20Vetter%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20provided%20WQL%20query%20didn't%20work%20in%20my%20environment%20(MECM%202107).%3C%2FP%3E%3CP%3ERequired%20adjustment%3A%3CEM%3E%20R.Client%20%3D%20null%20%3D%26gt%3B%26nbsp%3BR.Client%20%3CSTRONG%3Eis%3C%2FSTRONG%3E%20null%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3ESelect%20R.ResourceID%2CR.ResourceType%2CR.Name%2CR.SMSUniqueIdentifier%2CR.ResourceDomainORWorkgroup%2CR.Client%20from%20SMS_R_System%20as%20r%20full%20join%20SMS_R_System%20as%20s1%20on%20s1.ResourceId%20%3D%20r.ResourceId%20full%20join%20SMS_R_System%20as%20s2%20on%20s2.Name%20%3D%20s1.Name%20where%20s1.Name%20%3D%20s2.Name%20and%20s1.ResourceId%20!%3D%20s2.ResourceId%20and%20R.Client%20%3CSTRONG%3Eis%3C%2FSTRONG%3E%20null%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294521%22%20slang%3D%22en-US%22%3ERemoving%20Duplicate%20Device%20Objects%20from%20Configuration%20Manager%20with%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294521%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone%2C%20Chris%20Vetter%20Customer%20Engineer%20at%20Microsoft%20back%20again%20to%20discuss%20an%20all-too-common%20problem%20and%20that%20is%20Duplicate%20Device%20Hostnames%20in%20Microsoft%20Endpoint%20Manager%20Configuration%20Manager%20(MEMCM).%20This%20commonly%20tends%20to%20happen%20when%20performing%20an%20OSD%20Image%20on%20a%20known%20device%20(known%20meaning%20the%20device%20already%20exists%20inside%20your%20database)%20without%20deleting%20the%20object%20out%20of%20Active%20Directory.%20What%20we%20get%20is%20duplicate%20device%20objects%20which%20can%20cause%20conflicts%20when%20updating%20policy%20or%20collecting%20inventory%20and%20the%20device%20record%20you%20want%20updated%20is%20not%20because%20the%20action%20was%20performed%20on%20the%20duplicate%20that%20is%20not%20actually%20associated%20with%20the%20client.%3C%2FP%3E%0A%3CP%3EThere%20is%20no%20automatic%20or%20supported%20way%20to%20detect%20and%20remove%20these%20duplicate%20objects%20but%20there%20is%20a%20workaround%20that%20you%20can%20use%20and%20even%20automate%20depending%20on%20how%20large%20of%20an%20issue%20this%20is%20in%20your%20environment.%20We%20are%20going%20to%20create%20a%20Device%20Collection%20of%20the%20duplicate%20device%20hostnames%20then%20use%20a%20PowerShell%20script%20to%20remove%20all%20the%20objects%20from%20the%20collection%20(and%20the%20Database)%20this%20way%20the%20correct%20object%20can%20be%20added%20back%20through%20discovery.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%201%3A%20Create%20Device%20Collection%20of%20Duplicate%20Hostnames%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EI%20am%20going%20to%20assume%20you%20know%20how%20to%20create%20a%20device%20collection%20and%20if%20you%20do%20not%20there%20are%20many%20great%20articles%20and%20documents%20on%20the%20web%20that%20can%20show%20you%20how%2C%20so%20in%20this%20step%20I%20will%20provide%20the%20WQL%20query%20you%20can%20use%20to%20populate%20the%20collection%20with%20the%20duplicate%20hostnames.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3ESelect%20R.ResourceID%2CR.ResourceType%2CR.Name%2CR.SMSUniqueIdentifier%2CR.ResourceDomainORWorkgroup%2CR.Client%20from%20SMS_R_System%20as%20r%20full%20join%20SMS_R_System%20as%20s1%20on%20s1.ResourceId%20%3D%20r.ResourceId%20full%20join%20SMS_R_System%20as%20s2%20on%20s2.Name%20%3D%20s1.Name%20where%20s1.Name%20%3D%20s2.Name%20and%20s1.ResourceId%20!%3D%20s2.ResourceId%20and%20R.Client%20is%20null%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%202%3A%20Run%20the%20PowerShell%20to%20remove%20the%20objects%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20an%20easy%20one%20liner%20you%20can%20run%20after%20you%20connect%20to%20the%20PS%20Drive%20of%20your%20MEMCM%20Site%20which%20you%20should%20be%20able%20to%20do%20from%20any%20machine%20where%20you%20have%20the%20MEMCM%20Admin%20Console%20installed.%3C%2FP%3E%0A%3CP%3EGet-CMCollectionMember%20-CollectionName%20%22%3CCOLLECTION%20name%3D%22%22%20here%3D%22%22%3E%22%20%7C%20Remove-CMDevice%20-Force%3C%2FCOLLECTION%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDepending%20on%20how%20many%20objects%20are%20on%20your%20collection%20will%20determine%20how%20long%20it%20will%20take%20for%20the%20script%20to%20complete.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%203%3A%20Automate%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIf%20this%20is%20a%20continuous%20problem%20in%20your%20environment%2C%20I%20would%20recommend%20running%20this%20script%20on%20a%20schedule.%20You%20can%20download%20the%20complete%20script%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fcvetter07%2FRemoveDuplicateDevicesinMEMCM%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E%20that%20will%20connect%20to%20the%20MEMCM%20PS%20Drive%20and%20run%20the%20PowerShell%20line%20to%20remove%20the%20objects.%20How%20often%20you%20need%20to%20run%20it%20would%20depend%20on%20how%20quickly%20the%20collection%20you%20created%20repopulates.%20We%20really%20should%20get%20in%20the%20practice%20of%20tuning%20our%20discovery%20methods%20and%20removing%20the%20device%20from%20AD%20to%20minimize%20this%20issue.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EDisclaimer%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3EAll%20processes%20and%20directions%20are%20of%20my%20own%20opinion%20and%20not%20of%20Microsoft%20and%20are%20from%20my%20years%20of%20experience%20with%20the%20configuration%20manager%20product%20in%20multiple%20customer%20environments.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3E%3CSTRONG%3EResources%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fconfigurationmanager%2Fget-cmcollectionmember%3Fview%3Dsccm-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGet-CMCollectionMember%20(ConfigurationManager)%20-%20Configuration%20Manager%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fmem%2Fconfigmgr%2Fcore%2Fservers%2Fmanage%2Fcreate-queries%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20queries%20-%20Configuration%20Manager%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3294521%22%20slang%3D%22en-US%22%3E%3CP%3ELearn%20how%20to%20remove%20duplicate%20objects%20from%20you%20Configuration%20Manager%20environment%20easily%20and%20quicky%20with%20a%20little%20PowerShell%20script%20that%20you%20can%20set%20up%20easily%20to%20run%20on%20a%20schedule.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3294521%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EChrisVetter%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3405075%22%20slang%3D%22en-US%22%3ERe%3A%20Removing%20Duplicate%20Device%20Objects%20from%20Configuration%20Manager%20with%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3405075%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20call%20out%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F246981%22%20target%3D%22_blank%22%3E%40Birdy%3C%2FA%3E.%20I%20have%20fixed%20the%20query.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎May 20 2022 04:41 AM
Updated by: