Skip to main content

Imports Users in Active Directory to SQL Server

' Bind to RootDSE - this object is used to
' get the default configuration naming context
' e.g. dc=wisesoft,dc=co,dc=uk
set objRootDSE = getobject("LDAP://RootDSE")

' Root of search set to default naming context.
' e.g. dc=wisesoft,dc=co,dc=uk
' RootDSE saves hard-coding the domain.
' If want to search within an OU rather than the domain,
' specify the distinguished name of the ou. e.g.
' ou=students,dc=wisesoft,dc=co,dc=uk"
strRoot = objRootDSE.Get("DefaultNamingContext")

' Filter for user accounts - could be modified to search for specific users,
' such as those with mailboxes, users in a certain department etc.
strfilter = "(&(objectCategory=Person)(objectClass=User))"

' Attributes to return from the query
strAttributes = "sAMAccountName,userPrincipalName,givenName,sn," & _
"displayName,mail, " & _
"title,department," & _
"manager"

'Scope of the search. Change to "onelevel" if you didn't want to search child OU's
strScope = "subtree"

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

cn.open "Provider=ADsDSOObject;"
cmd.ActiveConnection = cn
cmd.commandtext = ";" & strFilter & ";" & _
strAttributes & ";" & strScope

set rs = cmd.execute

' Copy Field names to header row of worksheet

'Object for SQLSERVER
set cn2 = CreateObject("ADODB.Connection")

cn2.open "Provider=SQLNCLI;Server=SERVER_NAME;Database=DB_NAME;Trusted_Connection=yes;"

If rs.RecordCount > 0 then

rs.MoveFirst

cn2.execute "TRUNCATE TABLE UsersInAD"

While not rs.EOF

cn2.execute _
"INSERT INTO UsersInAD ([sAMAccountName], [userPrincipalName], [givenName], " & _
"[sn], [displayName], [mail], " & _
"[title], [department], [manager] ) " & _
"VALUES (" & _
"'" & rs.Fields("sAMAccountName") & "','" & rs.Fields("userPrincipalName") & "','" & rs.Fields("givenName") & "'," & _
"'" & rs.Fields("sn") & "','" & rs.Fields("displayName") & "','" & rs.Fields("mail") & "'," & _
"'" & rs.Fields("title") & "','" & rs.Fields("department") & "','" & rs.Fields("manager") & "'" & _
")"

rs.MoveNext
Wend

end If


rs.close
cn.close

set rs = Nothing
set cn = Nothing

cn2.Close
set cn2 = Nothing


PS: don't forget to adjust Table name, SQL Server Name, Database Name

Comments

Popular posts from this blog

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then

Could not load file or assembly error in VS 2010 and CR

when u received this kind of error "Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified." try to add this to your .config file (usually app.config) PS: don't forget to remove extra space after " see u

SBO - Internal Error (-5002) Occured in Inventory Transfer

this is something that not well documented in SBO, it took me several hours to look at Google and can't find anything about it. Thank God friend of mine got answer for this one.So the Sympton is something like this, u need to make Inventory Transfer for Batch Item and Serial Item in one single Transaction so the Transaction is the combination of it. ex in detail lines: 1. Serial Item 2. Batch Item 3. Batch Item 4. Batch Item 5. Serial Item 6. Serial Item . When u reach this code oStockTransfer.Add it will return an error "Internal Error (-5002) Occured". This is because ur details is not in order, meaning u need to order the line with combination of Batch first then Serial or Serial first then Batch so be careful with this thing :D see u