If you are using MDT, I highly recommend using the MDT database to streamline your imaging process. Back in November, we covered a method to automatically import computers from Active Directory into your MDT database. This script has one drawback. It can accidently create duplicate entries if you physically move your computers around.
Like most global issues, PowerShell can fix this! The script below can help you by removing duplicate computers form your MDT database. You will need the MDT PowerShell module for this script to run. You can either import it within the script (line 1) or have it import in your PowerShell profile. I prefer the PowerShell profile route.
Import-Module –name ".\MDTDB.psm1" Connect-MDTDatabase –sqlServer MDTDATABASE.Test.local –database MDT #Remove Old Duplicate Entries $MDTComputers = Get-MDTComputer | sort id foreach ($MDTComputer in $MDTComputers){ $SerialNumber = Get-MDTComputer -serialNumber $MDTComputer.SerialNumber $AssetTag = Get-MDTComputer -assetTag $MDTComputer.AssetTag if (($SerialNumber).count -gt 1 -and $MDTComputer.SerialNumber -ne ""){ write-host "Duplicate Serial Number Found:" $MDTComputer.AssetTag Remove-MDTComputer -id $MDTComputer.ID } if (($AssetTag).count -gt 1 -and $MDTComputer.AssetTag -ne ""){ write-host "Duplicate Serial Number Found:" $MDTComputer.AssetTag Remove-MDTComputer -id $MDTComputer.ID } if ((Get-QADComputer -Name $MDTComputer.AssetTag) -eq $Null){ write-host "No AD Account Found for " + $MDTComputer.AssetTag Remove-MDTComputer -id $MDTComputer.ID } $MDTComputers = Get-MDTComputer | sort id } }}
You will need to edit the Connect-MDTDatabase line and plug in your MDT Database server name. Other than that, no configuration should be required. The script will cycle through your MDT database and remove duplicate IDs based on the AssetTag and the SerialNumber. If a duplicate item is found, it will assume that the older (lesser) ID is obsolete. It will remove that ID.
This script can be ran separately from your importing script or appended to the end of it. When your import script (and this script) are scheduled to run daily, any computer changes made in Active Directory will be updated in your MDT database! This makes imaging maintenance automated. If you have any issues (or comments), let me know!
Have you got a working example of a script that would check to see if the computer already exists in the database and then create/update as required?
I don’t – I don’t have to use MDT very much anymore so I don’t even have a good test environment for it.
What happens if there are duplicate entries for a computer name in the MDT database? For instance, lets say that I have two entries for the same mac address, with different computer names, or a similar scenario. Which one is chosen by ZTIGather?
Thanks,
The smaller ID will win. MDT will match it first and apply those settings.