Rightio people's, I have a problem.
My scripting knowledge is very limited (And I intend to keep it that way) however I need to add to a script at work to fix a problem we have.
Long story short, we use Microsoft SMS to roll out our SOE images and once the OS has been built, we run a script which does all the configuration. A little while ago we found that some computers were being built and added to the domain with the name “Telstra” and a random number sequence instead of “NML” followed by the Dell service tag, after some problem analysis we found that a certain part of the script (The part that renames the machine and rejoins the domain, was failing if the computer already exists in AD, seeing as how netdom cannot remove objects from AD I need to add something to the script before that step which searches AD for the machine and deletes it if something is found.
So basically, my script can find the Dell Service tag (called “sAssetTag” in the script) and I can search through AD and delete an object based on the location details that are found by the search, however I can’t figure out how to tell the search to find a computer object with the name ‘sAssetTag’, if I specifically state what the name is I am looking for, such as “8DFGN1S” the whole thing works, but I want the search to find a computer in AD based on the result of “sAssetTag”. Effectively making it a one script fits all sort of thing.
Hope that explains it….
Anyways, below is the script I am using:
I’m sure it’s not the perfect script, and I can probably take some things out and shorten it, however it (sort of) works and I don’t know enough about scripting to make it more efficient!
Quote:
Dim sAssetTag, strComputer, objWMIService, colComputers
Dim objComputer, colBIOS, objBIOS, Err
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Obtain Asset Tag
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For Each objBIOS In colBios
sAssetTag = objBIOS.SerialNumber
Next
'Delete Computer object from domain if it exists
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select ADsPath from " &_
"'LDAP://DC=Domain,DC=com,DC=au' where objectClass='computer'" &_
" and name = (Computername here) "
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
if objRecordSet.EOF then
cfound=false
Wscript.Echo "not cool"
Wscript.echo sassettag
else
cfound=true
Wscript.Echo objRecordSet.Fields("ADsPath").Value
Wscript.Echo "cool"
set objComputer = GetObject(objRecordSet.Fields("ADsPath").Value)
objComputer.DeleteObject (0)
end if
|
So yeah, as you can see, the
(Computername here) part is where I specify what to search for, and I’m not sure how to tell it to look for sAssetTag (‘sAssetTag’) doesn’t work…..
I removed the Domain name intentionally…
And don’t worry about the “cool” and “not cool” parts [img]tongue.gif[/img] That’s just to tell me what part it’s getting to!
Any help would be great!
[ 06-26-2007, 03:33 AM: Message edited by: Hivetyrant ]