Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script Active directory to get computer name and Operating System type

Posted on 2008-06-19
13
Medium Priority
?
458 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

670 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