Share via


Removing old computer object in AD during OSD?

Question

Thursday, December 3, 2015 3:06 PM

Hi,

I'm looking for a way to delete old computer objects from AD during OSD.
The scenario is this:

I am migrating from SCCM 2007 to 2012 R2.
The old naming standard for computers were WS-%macaddress%
The new naming standard is supposed to be LKP-A%serialnumber%

For the time being, I set the new naming standard with CustomSettings.ini and it works fine.

The problem is that whenever a computer is reinstalled, the old computer object in AD remains.

How can I solve this in an easy way?
Preferable not removing my CustomSettings (the Gather step)

I've been looking at various PowerShell scripts, but I know NOTHING about Powershell, so it always ends with doing something else.
So if possible, I would like to avoid using PowerShell.

Thanks in advance

All replies (11)

Saturday, December 5, 2015 4:26 PM âś…Answered

This bit of VBscript will get the active MAC address, append your "WS-" to the beginning and then set the OSDComputerName variable in SCCM (assuming you then want to delete it from AD);

Computer = "."
Set OBJWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\" & Computer & "\root\cimv2")
Set Macs=OBJWMIService.execquery("select MacAddress from Win32_NetworkAdapter where MacAddress is not null and PhysicalAdapter = true and NetConnectionStatus = 2 and NetEnabled = true")
OSDComputerName = ""
for each Mac in Macs
OSDComputerName = replace( "WS-" & cstr(Mac.MacAddress),":","")
'wscript.echo OSDComputerName
exit for
next
Set TSEnv=CreateObject("Microsoft.SMS.TSEnvironment")
TSEnv("OSDComputerName") = OSDComputerName

Personally, I don't delete computer objects when re-imaging (in your case, the name change is what is driving it I assume?) because most re-imaging tasks for the same computer are a break/fix situation, wherere any groups it is a member of, and the OU it belongs to, remain the same...

Jack


Friday, December 4, 2015 5:37 AM

Dear Sir,

Below is an example of acting the active directory. What you need is to customize the .vbs file to delete PC name from AD. I'm not a programmer, something like this:

strComputer = variable
set objComputer = GetObject("LDAP://CN=" & strComputer & _   ",CN=Computers,DC=ABC,DC=Lab")
objComputer.DeleteObject (0)

http://ccmexec.com/2010/12/move-computer-to-the-correct-ou-during-deployment/

Best regards

Frank


Friday, December 4, 2015 7:29 AM

Thank you Frank,

As I have to enter a variable for the current name, do you have a suggestion how a variable is supposed to look?

Again, the the naming standard that should be deleted from AD is: WS-%macaddress%

VB variables isn't the same as MDT variables.


Monday, December 7, 2015 2:09 PM

This bit of VBscript will get the active MAC address, append your "WS-" to the beginning and then set the OSDComputerName variable in SCCM (assuming you then want to delete it from AD);

Computer = "."
Set OBJWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\" & Computer & "\root\cimv2")
Set Macs=OBJWMIService.execquery("select MacAddress from Win32_NetworkAdapter where MacAddress is not null and PhysicalAdapter = true and NetConnectionStatus = 2 and NetEnabled = true")
OSDComputerName = ""
for each Mac in Macs
OSDComputerName = replace( "WS-" & cstr(Mac.MacAddress),":","")
'wscript.echo OSDComputerName
exit for
next
Set TSEnv=CreateObject("Microsoft.SMS.TSEnvironment")
TSEnv("OSDComputerName") = OSDComputerName

Personally, I don't delete computer objects when re-imaging (in your case, the name change is what is driving it I assume?) because most re-imaging tasks for the same computer are a break/fix situation, wherere any groups it is a member of, and the OU it belongs to, remain the same...

Jack

Thanks Jack!

So I can simply put this somewhere in the beginning of the task sequence, and then proceed with the rest of steps (like format the disk, apply OS, set computername (the correct one, with Gather/customsettings.ini) etc?
i.e I don't have to change anything in the vbs?

To answer your question: well, no. The name change isn't a must really. But if/when the computer needs to be reinstalled, they want to take the opportunity to change the name at the same time.


Monday, December 7, 2015 5:06 PM

All this script does is set the TS Environment "OSDComputerName" to the current computer name in the format of "WS-<MAC>", you can then run a second script to delete the computer name from AD using an account that has permissions and the same variable we just set. You would normally run this script early in WinPE so the AD object is deleted before you re-create it...

Jack


Thursday, December 10, 2015 10:24 AM

Thanks for clearing that out Jack!

If you would happen to have a script removing the computer from AD, it would be appreciated as well :)


Thursday, December 10, 2015 3:59 PM

All this script does is set the TS Environment "OSDComputerName" to the current computer name in the format of "WS-<MAC>", you can then run a second script to delete the computer name from AD using an account that has permissions and the same variable we just set. You would normally run this script early in WinPE so the AD object is deleted before you re-create it...

Jack

Jack, your script works very good!
However, my current idea is to export the WS-%macaddress% to a textfile on a local share instead.
And make take the cleanup from there instead. Is it possible to modify your script for this purpose?

Using your script right now, doesn't allow me to overwrite is with the "real" Gather/CustomSettings.ini step.

Also, if I use your script to set the hostname, and then try to export it, I will get a MININT name, as the "real" name doesn't show in PE.

So, do you think you could help me rewriting the script, so simply put out the result to a textfile in a share, rather than setting it to it's hostname?

(of course, if i've missed something you're more than welcome to help me out)


Thursday, December 10, 2015 4:32 PM

It doesn't care what the name of the PC is, it gets the MAC address and appends "WS-", no matter if in PE or full Windows...

Jack


Friday, December 11, 2015 9:32 AM

So instead of adding WS-%macaddress% as the hostname, is it possible to simply pipe out the "result"/value to a text instead?


Friday, December 11, 2015 1:09 PM

I already set a TS variable with the name, you can then do anything you want with it, use it in the TS (it over-rides the name specified in "unattended" automatically so if left as-is will become the new name for the image) or use in another vbscript (like to delete the object from AD as initially requested)...

Jack


Thursday, January 7, 2016 6:59 AM

I actually ended up making a Powershell script.. actually two of them.

The first took the old name, and piped it into a txt-file on a network share.
This was done automatically during the TS.

The second script is currently a scheduled task, which uses the Active Directory module to "connect" to the AD and wipe the object.