Jerold
Tue Feb 21 08:47:20 CST 2006
On 21 Feb 2006 05:08:34 -0800, "Karl Stringer" <karl.stringer@filcs.com> wrote:
>I'm trying to put together a script that will delete any computer
>accounts that match a particular naming convention (this is in a
>Windows 2003 domain). All the computers that I'm trying to remove are
>named test* e.g. testaa, test01, testxyz9 etc.
>
>I've tried the following:
>
>================================================
>Dim Container
>Dim ContainerName
>Dim ComputerToRemove
>ContainerName = "MyDomain"
>Set Container = GetObject("WinNT://" & ContainerName)
>ComputerToRemove = "test*"
>Call Container.Delete("Computer", ComputerToRemove)
>================================================
>
>But of course it doesn't work because it can't recognise test*.
>
>I don't want to have to call a text file with all the names of the
>test* computers as it would be fairly dynamic with new ones being added
>all the time.
>
>Can anyone help?
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, DOM
Const ADS_ACETYPE_ACCESS_ALLOWED_OBJECT = &H5
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(objectClass=computer))"
strAttributes = "distinguishedName, name"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Set oShell = CreateObject( "WScript.Shell" )
DOM=oShell.ExpandEnvironmentStrings("%USERDOMAIN%")
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName")
strName = objRecordSet.Fields("Name")
Set objSdUtil = GetObject("LDAP://" & strDN)
wrk1 = Mid(strName,1,4)
wrk2 = LCase(wrk1)
If wrk2 = "test" then
Wscript.Echo "Delete this computer account " & strDN
End If
objRecordSet.MoveNext
Loop
objConnection.Close
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com