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
'*******************************************************
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:
شركة الصفرات
شركة الصفرات لكشف تسربات المياه الصفرات
شركة الصفرات للتنظيف بالرياض
شركة الصفرات لمكافحة الحشرات
شركة الصفرات لنقل الاثاث
شركة الصفرات لتسليك المجارى بالرياض
شركة الصفرات لتنظيف الخزانات بالرياض
شركة الصفرات لتنظيف الشقق بالرياض
شركة الصفرات لتنظيف الفلل بالرياض
By Unknown, at 7:02 PM
Very good post. I'm facing some of these issues as well..
http://el-tamimi.com/
http://el-tamimi.com/legal-consultants/
http://el-tamimi.com/divorce-lawyer/
http://el-tamimi.com/best-lawyer/
http://el-tamimi.com/lawyer-number/
By Unknown, at 6:20 PM
شركة شفط بيارات بعنيزة
شركة كشف تسربات المياه بعنيزة
شركة تسليك مجاري بعنيزة
شركة جلي بلاط بعنيزة
By furnitureswrry, at 3:38 PM
Terima kasih artikelnya sangat membantu sekali mudah-mudahan artikel ini bermanfaat bagi semua.
Obat Herbal Untuk Penyakit Chikungunya
Obat Flu Dan Batuk Anak
Obat Herbal Untuk Sakit Perut Melilit
By Cara Mengatasi Penyakit Kuning Yang Tak Kunjung Sembuh, at 8:53 AM
I definitely love this site.
http://prokr2017.angelfire.com/
http://prokr.over-blog.com/
https://sites.google.com/site/prokrservices/
https://www.prokr.net/ksa/jeddah-water-leaks-detection-isolate-companies/
By lamiss ibrahim, at 6:01 AM
Post a Comment
<< Home