This blog is by design...

Friday, June 17, 2005

Script to write all server names to a file using SQL type query

This is a simple little script that will generate a text file of server and DC names sorted in alphabetical order. In my Win2K domain, all my servers (other than DC's, of course) reside in an OU called Servers that is in the root of my OU structure. I have several child OU's under the Servers OU, but this script accounts for that so we don't need to worry about doing any kind of OU recurse. We're just going to use a SQL type query to query the entire domain with an ADODB connection for any computer object with the word "Servers" in it's distinguishedName. I figured most people probably have a similar setup (all your servers in one OU), so just change the strServersCont variable to match the name of the OU you keep your servers in and the script should work for you as well.

I've also included code to pull in the names from the default Domain Controllers OU. We then use a totally separate recordset to sort the first recordset by name, and voila. We have a nice little text file, c:\servers.txt, with the names of all servers and all DC's. I have several other scripts that get their data from this text file and I didn't want to have to worry about updating the file every time we added/removed servers, so I have this script set up as a daily task. One less thing I have to worry about :)

'***************************************************
Const ForWriting = 2
Const OpenAsASCII = 0
strServersCont = "Servers"
strOutputFile = "c:\servers.txt"

Set objRootDSE = GetObject("LDAP://rootDSE")
strDomain = "LDAP://"& objRootDSE.Get("defaultNamingContext")

Set objFSO = CreateObject("scripting.filesystemobject")
Set objOutputFile = objFSO.CreateTextFile _
(strOutputFile, ForWriting, OpenAsASCII)

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.CommandText = "<"& strDomain & _
">;(objectCategory=computer)" & ";distinguishedName,cn;subtree"
objCommand.Properties("Page Size") = 10000

Const adVarChar = 200 'specifies a string type data
Set objRecordsetNew = CreateObject("ADODB.Recordset")
objRecordsetNew.Fields.Append "ServerName", adVarChar, 50
objRecordsetNew.Open

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
if instr(objRecordSet.Fields("distinguishedName"), strServersCont) _
<> 0 OR instr(objRecordSet.Fields("distinguishedName"), _
"Domain Controllers")then
objRecordsetNew.AddNew
objRecordSetNew("ServerName") = objRecordset.Fields.Item("cn").Value
strCount = strCount + 1
end if
objRecordSet.MoveNext
Wend
objConnection.Close

objRecordSetNew.Sort = "ServerName ASC"

While Not objRecordSetNew.EOF
objOutputFile.writeline objRecordSetNew.Fields.Item("ServerName")
objRecordSetNew.Movenext
Wend

objOutputFile.Close

set objConnection = Nothing
set objCommand = Nothing
set objRecordSet = Nothing
set objRecordSetNew = Nothing
set objOutputFile = Nothing
set objFSO = Nothing
'*******************************************************

5 Comments:

Post a Comment

<< Home