Solved

Script Active directory to get computer name and Operating System type

Posted on 2008-06-19
13
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A hard and fast method for reducing Active Directory Administrators members.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

710 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