Question : How to list the machine accounts in specific OU and export it into excel sheet?

Hi all,

Currently, i use the "DSQuery" utility to query the machine accounts (specially servers) in specific OU. However, i don't like the output of this command as it shows the machines by their DN names in a messy way and it doesn't provide more details about the machine accounts such as OS.  I seek your help if you can provide me with PS or VBS script that can allow me to output the report like the following format :

Machine Name                         OU                         Domain Name                    OS
MySrv01                                  Servers                    Mytest.com                      Win2003
MySrv02                               Servers/Exchange       Mytest.com                     Win2008


As mentioned above for Mysrv02, i need the script to look to sub-OUs such as Exchange OU under the Servers OU

Appreciate your quick response and prompt action.

Thanks

Answer : How to list the machine accounts in specific OU and export it into excel sheet?

Hi there,

Just to provide a script which is exactly what you asked for without needing modifications, save the below as a vbs file and run it via cscript from the command line, passing the results to a CSV file. Specify your OU using the /ou switch...

cscript getComputers.vbs /ou:"ou=someOU,dc=domain,dc=local" > results.csv

..then open results.csv in Excel and the report will be here.

Tony
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
strOUBase = WScript.Arguments.Named("ou")

Set objConn = CreateObject("ADODB.Connection")
Set objComm =   CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objComm.ActiveConnection = objConn
objComm.Properties("Page Size") = 1000
objComm.Properties("Searchscope") = 2
objComm.Properties("Sort on") = "name"

objComm.CommandText = "SELECT name,distinguishedName From 'LDAP://" & strOUBase _
	& "' WHERE objectClass='computer'"
Set objRS = objComm.Execute

If objRS.RecordCount > 0 Then
	objRS.MoveFirst
	WScript.Echo "Machine Name,OU,Domain,OS"
	Do Until objRS.EOF
		strDN = Replace(objRS.Fields("distinguishedName").Value,"/","\/")
		Set objPC = GetObject("LDAP://" & strDN)
		arrCNInfo = Split(getCNFromDN(strDN),"/")
		strName = arrCNInfo(UBound(arrCNInfo))
		strDomain = arrCNInfo(0)
		For i = 1 To UBound(arrCNInfo)-1
			strOU = strOU & arrCNInfo(i) & "/"
		Next
		If Right(strOU,1) = "/" Then strOU = Left(strOU,Len(strOU)-1)
		strOS = objPC.operatingSystem
		WScript.Echo strName & "," & strOU & "," & strDomain & "," & strOS
		strOU = ""	
		objRS.MoveNext
	Loop
End If

Function getCNFromDN(strDN)
 Const ADS_NAME_INITTYPE_GC = 3
 Const ADS_NAME_TYPE_1779 = 1
 Const ADS_NAME_TYPE_CANONICAL = 2
 Set objNT = CreateObject("NameTranslate")
 objNT.Init ADS_NAME_INITTYPE_GC, ""
 objNT.Set ADS_NAME_TYPE_1779, strDN
 getCNFromDN = objNT.Get(ADS_NAME_TYPE_CANONICAL)
End Function
Random Solutions  
 
programming4us programming4us