Solved

Script Active directory to get computer name and Operating System type

Posted on 2008-06-19
13
450 Views
Last Modified: 2011-10-19
I need to get a list of all the servers we have on lan and the operating system that they have installed
Can I run a script against AD to get this information

Does anyone have any good way to get this info
0
Comment
Question by:neoptoent
13 Comments
 
LVL 58

Expert Comment

by:tigermatt
ID: 21823190
How many servers do you have? If it's only a few, you could just look at the properties of the objects of these servers in Active Directory. It will tell you OS and Service Pack information in there, which means you don't need a script.
0
 

Author Comment

by:neoptoent
ID: 21823342
There are a few hundred machines

I cant do it manually
0
 
LVL 40

Expert Comment

by:coolsport00
ID: 21823426
Yes..via VBScript. It places the info in an excel .csv file on the root of C:\. You can then modify it as needed. Just copy and paste the following in notepad, put your org name in where I have CAPS, save it as .vbs file and run it:
_________________________________________________________________________________________
Const ADS_SCOPE_SUBTREE = 2
Const LOGFILE = "C:\OSverScript.csv"  ' Log file path and name

' ** Create the database connection, command, and recordset objects

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
Set objRecordset = CreateObject("ADODB.Recordset")

' ** Create and open the text file

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(LOGFILE,True)

' ** Set the provider and open Active Directory

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

' ** Set the properties of the command object to select the OS and Version

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
                "SELECT Name, operatingSystem, operatingSystemVersion " _
                & "FROM 'LDAP://DC=YOURDOMAIN,DC=COM' " _
                & "WHERE objectClass='computer' " _
        & "ORDER BY operatingSystem"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

' ** Execute the command to get the Active Directory info
' ** Place the data in the recordset

Set objRecordset = objCommand.Execute

' ** Write the values of the recordset to the text file

objRecordset.MoveFirst      ' Point to first record
Do Until objRecordset.EOF
      strLine = ""
      
      ' ** Add the computer name to the output line
      strValue = ""
      If Not IsNull(objRecordset.Fields("Name").Value) Then _
            strValue = CStr(objRecordset.Fields("Name").Value)
      strLine = strLine & strValue & ","

      ' ** Add the Operation system to the output line
      strValue = ""
      If Not IsNull(objRecordset.Fields("operatingSystem").Value) Then _
            strValue = CStr(objRecordset.Fields("operatingSystem").Value)
      strLine = strLine & strValue & ","
      
      ' ** Add the Operation system version to the output line
      strValue = ""
      If Not IsNull(objRecordset.Fields("operatingSystemVersion").Value) Then _
            StrValue = CStr(objRecordset.Fields("operatingSystemVersion").Value)
      strLine = strLine & strValue
      
      ' ** Write the line to the text file
      objFile.WriteLine(strLine)
  objRecordset.MoveNext            ' next record
Loop

' ** Close the text file, recordset and connection, and release all the objects

objFile.Close
objRecordset.Close
objConnection.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objRecordset = Nothing
Set objConnection = Nothing
Set objCommand = Nothing
Set objRecordset = Nothing
__________________________________________________________________________________________


I know it looks overwhelming, but you don't have to do anything but change your domain name above.

Hope that helps.

Regards,
~coolsport00
0
 

Author Comment

by:neoptoent
ID: 21823552
Hi,

when I do this I get error line 24 character 1 table does not exist
0
 

Author Comment

by:neoptoent
ID: 21823570
Line 34 sorry
0
 
LVL 40

Expert Comment

by:coolsport00
ID: 21823679
I re-ran the code here and it worked with no errors. VB, like any coding is VERY picky. :)  Make sure you look at the code I pasted and that you pasted it with all the "ticks" ('), quotes, and spaces. The ticks are used in front of lines for commenting (i.e. it's not actual VB code that runs). I assume you changed the LDAP part above? ("FROM 'LDAP://DC=YOURDOMAIN,DC=COM' " _)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Accepted Solution

by:
Henrik Johansson earned 500 total points
ID: 21823749
Use CSVDE to get a CSV-file with the result.
C:\>csvde -f output.csv -d dc=domain,dc=com -r "(objectClass=computer)" -l name,operatingsystem
0
 

Author Comment

by:neoptoent
ID: 21823756
I tried to recopy it and still got th error .

Can you try to put it in again as "attach code snippet"

Maybe that will make a diff?
0
 
LVL 40

Expert Comment

by:coolsport00
ID: 21823798
Here you go...try this. :)
GetOSVer.txt
0
 

Author Comment

by:neoptoent
ID: 21823814
Do I need to run this on the dc?

Zach
0
 
LVL 40

Expert Comment

by:coolsport00
ID: 21823849
I have the Win2K3 Admin Tools (and thus AD U&C) on my laptop so I run it from here and it works. I didn't try running it on a regular desktop. I had one of my developers write that for me as I'm not a huge VB scriptor...can only make out what the script is doing. I write smaller, easier scripts. :)

Try running it on a DC or at least on a PC that has the admin tools on it and see if it works. I assume you're still getting the error?
0
 

Author Comment

by:neoptoent
ID: 21823884
The error has moved to line 41 now
0
 

Author Comment

by:neoptoent
ID: 21823966
The CSVDE has worked. Thanks for all with the help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now