Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script Active directory to get computer name and Operating System type

Posted on 2008-06-19
13
Medium Priority
?
460 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses

971 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