[download id=”40″]

When we are deploying OS via ConfigMgr, we usually do a cleanup of all old objects before creating the new object.

This helps prevent errors and mistakes, and is pretty easy to do.

We are able to use both SMBOIS GUID and Mac addresses for the cleanup.

usually I recommend using mac addresses, since I have experienced that laptops who have had their mainboard exchanged, have the BIOS GUID of “0000-00000-0000-0000”, this happens because the repair shop forgets to write in the GUID after the repair.

The mac addresses are always usable , since they are not typed in by hand.

Problem by using the mac adresses is that there is more than on each, therefore I have made this script that does a cleanup of All computer objects in the SCCM, checking for All of the Mac Addresses.

This script can be used of difference purposes.

1. Trobuleshooting. if you are having problems with the configmgr account of a specific PC. Run this script on the PC, and the configmgr will be cleaned up, which might solve your problems.

2. Deployment. Run this script in WinPE as a part of new deployment of an already exiting PC, to make sure that all old objects are gone, before reinstalling.

 

' //***************************************************************************
' // ***** Script Header *****
' //
' // File:      CleanupLocalPCMac.vbs
' // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com
' // Purpose:   Cleanup all objects in ConfigMgr, using all local Mac Addresses
' //			
' //			NOTICE: SMBIOSGUID could have been used instead, but sometimes 
' //			a computer has a guid that only conains zeros, and it would cause 
' //			other objects to be deleted too.
' //
' //
' // CORETECH A/S History:
' // 1.0.0     JGS 24/03/2011  Created initial version.
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************
'//----------------------------------------------------------------------------
'//
'//  Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

SCCM_SERVER = "CTSCCM01"
strUsername = Null
strPassword = Null

Dim strSiteCode, swbemServices

'//----------------------------------------------------------------------------
'//  Main routines
'//----------------------------------------------------------------------------

' Set up a connection to the local provider.
WriteLog "Script started: " & Now

Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
'Connect to local SCCM
ConnectionStatus = Connect(swbemServices,strSiteCode,SCCM_SERVER,strUsername,strPassword)

If ConnectionStatus <> "OK" Then
	WriteLog "Connection to local ConfigMgr: " & SCCM_SERVER & " Problem" & vbCrLf & ConnectionStatus
Else
	WriteLog "Connection to local ConfigMgr etablished"
End if

CleanupComputerObjectForLocalPCByMac swbemServices

'//----------------------------------------------------------------------------
'//  Procedures
'//----------------------------------------------------------------------------

Function CleanupComputerObjectForLocalPCByMac(connection)
		'On Error Resume Next
		WriteLog "Checking for old computer objects via Mac and GUID..."
		'Delete via Mac Addresses
		
		'Setup local WMI connection
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
		
		Set IPConfigSet = objWMIService.ExecQuery("SELECT MACAddress, Caption FROM Win32_NetworkAdapterConfiguration " )
			For Each IPConfig In IPConfigSet
				' Build the query

				Mac = IPConfig.MacAddress
				'vmware machines all have the same Mac, skip them.
				If Not IPConfig.MacAddress = Null Then
					If InStr(UCase(IPConfig.Caption),"VMWARE") = 0 Then
						WriteLog "Checking adapter: " & IPConfig.Caption & " Mac:" & IPConfig.MacAddress
						sQuery = "SELECT * FROM SMS_R_System WHERE MacAddresses = '" & IPConfig.MacAddress & "'" 'and Active = 1"
						' Process the query
						'Get ResourceID
						Set oClients = connection.ExecQuery(sQuery)
						For Each oClient In oClients
							Err.Clear
							'Get the computer settings object.
							WriteLog "Deleting computerobject name: " & oClient.Name & " resourceID: " & oClient.ResourceID & " via MacAddress: " & IPConfig.MacAddress
							oClient.Delete_
							If Err.Number <> 0 Then
								WriteLog "Delete Error: " & Err.Description & " - Number: " & Err.Number	
								Err.Clear
							End If
						Next
					Else
						WriteLog "Skipping adapter: " & IPConfig.Caption & " Mac:" & IPConfig.MacAddress & " - VMWare default mac address"
					End If
				Else
				WriteLog "Skpping adapter: " & IPConfig.Caption  & " - no mac address"
				End If
			Next
End Function

Function Connect (swbemServices, sitecodevar, hostname, username, password)
	On Error Resume Next
	
	Set swbemServices = swbemLocator.ConnectServer(hostname, "root\sms",username,password)
	
	If Err <> 0 Then
		'Connect = "Error: " & Err.Number & " - " & Err.Description
		Connect = "Error: " & Err.Description
		Exit Function
	End If
	
	Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")
	
	If Err <> 0 Then
		'Connect = "Error: " & Err.Number & " - " & Err.Description
		Connect = "Error: " & Err.Description
		Exit Function
	End If
	
	For Each objLocation In providerLoc
		If objlocation.ProviderForLocalSite = True Then
			Set swbemServices = swbemLocator.ConnectServer(objlocation.Machine, "root\sms\site_" + objlocation.SiteCode,username,password)
			sitecodevar = objlocation.SiteCode
			If Err <> 0 Then
				'Connect = "Error: " & Err.Number & " - " & Err.Description
				Connect = "Error: " & Err.Description
				Exit Function
			End If
			
		End If
	Next
	
	Connect = "OK"
	
End Function

Function WriteLog(strMessage)
	Const ForAppending = 8
	
	strScriptLocation = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile(strScriptLocation & "cleanup.log", ForAppending, True)
	
	objTextFile.WriteLine strMessage
	
	objTextFile.Close
	
	Set objTextFile = Nothing
	Set objFSO = Nothing
End Function

'//----------------------------------------------------------------------------
'//  House Cleaning
'//----------------------------------------------------------------------------

Set swbemServices = Nothing
Set swbemLocator = Nothing

WriteLog "Script End"

'//----------------------------------------------------------------------------
'//  End Script
'//----------------------------------------------------------------------------