LeightonJames Posted May 20, 2015 Posted May 20, 2015 How would I go about querying AD for the next available computername and then naming a machine that? I can't use powershell as we cannot guarantee that PS is installed on all the machines and the script has to work on XP too. I have a batch file that I am using to do a number of other functions so I can call this script from there. Example: schoolWS-001 schoolWS-002 schoolWS-004 schoolWS-005 I have 2 machines to add, one will take 003 and the other will take 006 Sorry if this is a very simple process but my scripting skills do not extend very far and I've been asked to automate a lot of processes in order to streamline the way we work. Thanks
halbaradkenafin Posted May 20, 2015 Posted May 20, 2015 Sounds like you need an LDAP query, it's not something I've dealt with outside of Powershell but this Technet article describes it reasonably well from what I can see. I'm guessing you're doing some sort of machine deployment to need this, what are you using that requires this?
LeightonJames Posted May 20, 2015 Author Posted May 20, 2015 (edited) Thanks for the reply, we are splitting up our large network into individual sites and we are renaming the machines on site to make it easier to identify them (and to tidy up AD). I have found this script through my google-fu but I get an error when I try to run it: StrAvailableName = FindAvailableName("TestWS") Function FindAvailableName(StrPrefix) '***************************************** '* Search for an available computer name * '***************************************** Const ADS_SCOPE_SUBTREE = 2 Dim nCount, PCName,PCExist Dim objConnection, objCommand,objRecordSet,PCVar nCount = 1 PCName = StrPrefix & "0" & nCount PCExist = False Do Until PCExist Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Page Size") = 100 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 'Enter your LDAP search root for your domain here 'LDAP://dc=corp,dc=com' objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://dc=test-dc,dc=test,dc=local' WHERE objectCategory='computer' " & "AND name=' " & PCName & "'" Set objRecordSet = objCommand.Execute PCVar = "" if not (objRecordSet.EOF And objRecordSet.BOF) Then objRecordSet.MoveFirst Do Until objRecordSet.EOF 'WScript.Echo objRecordSet.Fields("ADsPath").Value PCVar = objRecordSet.Fields("ADsPath").Value objRecordSet.MoveNext Loop nCount = nCount + 1 if nCount < 10 then PCName = StrPrefix & "0" & nCount else PCName = StrPrefix & nCount End If IF PCVar = "" Then PCExist = True END IF Loop AvailablePCName = PCName End Function error is Line 51 character 1 Loop without Do Edited May 20, 2015 by LeightonJames
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now