Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Add objectGUID into this vbscript

Status
Not open for further replies.

bb103

IS-IT--Management
Feb 10, 2014
2
CA
Hi,

I'm a unix admin helping out the windows team on a few projects they have right now. My vbscripting is beyond rusty now since its been maybe 8+ years since I've done any. I was about to write a script that did exactly as below but after a quick google search i found one that someone had already done.

I'm not sure how to add to retrieve the GUID and SID from the AD. I tried adding the object groups and adding it in the attributes section but it's not working for me and in all honesty when it comes to windows scripts i'm at a loss.

Can someone help me out? I can't use powershell ( I know that would be easier but other users need to use this script and they wont have the same access as me)

Code:
OPTION EXPLICIT

dim FileName, multivaluedsep,strAttributes
dim strFilter, strRoot, strScope
dim cmd, rs,cn
dim objRoot, objFSO,objCSV
dim comma, q, i, j, mvsep, strAttribute, strValue

' ********************* Setup *********************

' The filename of the csv file produced by this script
FileName ="userexport.csv"
' Seperator used for multi-valued attributes
multivaluedsep = ";"
' comma seperated list of attributes to export
strAttributes = "sAMAccountName,givenName,sn,displayName,description,physicalDeliveryOfficeName," & _
		"telephoneNumber,mail,cn"

' Default filter for all user accounts (ammend if required)
strFilter = "(&(objectCategory=person)(objectClass=user))"
' scope of search (default is subtree - search all child OUs)
strScope = "subtree"
' search root. e.g. ou=MyUsers,dc=wisesoft,dc=co,dc=uk
' leave blank to search from domain root
strRoot = ""

' *************************************************
q = """"

set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn

if strRoot = "" then
	set objRoot = getobject("LDAP://RootDSE")
	strRoot = objRoot.get("defaultNamingContext") 
end if

cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";" & _
		  strAttributes & ";" & strScope 

'**** Bypass 1000 record limitation ****
cmd.properties("page size")=1000

set rs = cmd.execute
set objFSO = createobject("Scripting.FileSystemObject")
set objCSV = objFSO.createtextfile(FileName)

comma = "" ' first column does not require a preceding comma
i = 0 

' create a header row and count the number of attributes
for each strAttribute in SPLIT(strAttributes,",")
	objcsv.write(comma & q & strAttribute & q)
	comma = "," ' all columns apart from the first column require a preceding comma
	i = i + 1
next
On Error Resume Next
' for each item returned by the Active Directory query
while rs.eof <> true and rs.bof <> true
	comma="" ' first column does not require a preceding comma
	objcsv.writeline ' Start a new line
	' For each column in the result set
	for j = 0 to (i - 1)
		select case typename(rs(j).value)
		case "Null" ' handle null value
			objcsv.write(comma & q & q)
		case "Variant()" ' multi-valued attribute
			' Multi-valued attributes will be seperated by value specified in
			' "multivaluedsep" variable
			mvsep = "" 'No seperator required for first value
			objcsv.write(comma & q)
			for each strValue in rs(j).Value
				' Write value
				' single double quotes " are replaced by double double quotes ""
				objcsv.write(mvsep & replace(strValue,q,q & q))
				mvsep = multivaluedsep ' seperator used when more than one value returned
			next
			objcsv.write(q)
		case else
			' Write value
			' single double quotes " are replaced by double double quotes ""
			objcsv.write(comma & q & replace(rs(j).value,q,q & q) & q)
		end select
		
		comma = "," ' all columns apart from the first column require a preceding comma
	next
	rs.movenext
wend

' Close csv file and ADO connection
cn.close
objCSV.Close

wscript.echo "Finished"

If i run without
I get this error:
Code:
(79, 5) Microsoft VBScript runtime error: Invalid procedure call or argument
Which is this line:
Code:
objcsv.write(mvsep & replace(strValue,q,q & q))

I would like to add these:
Code:
objectGUID,objectSid,sIDHistory

But the GUID is most important.
 
also if it goes into this format is perfect: 8dddf058-03ef-4d8b-b7e4-f7cdf89f473b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top