Get all the Networks groups of your Active Directory

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Get all the Networks groups of your Active Directory

Post by flaith »

Just because i needed it
This is the code (you need ADOmate for the Excel part)

Code: Select all

;NetQueryDisplayInformation
;http://msdn.microsoft.com/en-us/library/aa370610%28VS.85%29.aspx
;
;group account
IncludePath "include\"
XIncludeFile "ADOmate.pbi"

EnableExplicit

#MAX_PREFERRED_LENGTH = -1

Structure NET_DISPLAY_GROUP
  grpi3_name.i
  grpi3_comment.i
  grpi3_group_id.i
  grpi3_attributes.i
  grpi3_next_index.i
EndStructure

Structure DISPLAY_GROUP
  name.s
  comment.s
  group_id.i
  attributes.i
EndStructure

Define *dcname, dcname$
Define NetQueryDisplayInformation
Define ReturnedEntryCount, *NetQueryDisplayInformation, index
Define total
Define result
Define t1,t2

Define gr_a$, gr_b$

Global *pBuf.NET_DISPLAY_GROUP
Global NewList group_account.DISPLAY_GROUP()

;-
;-Exportation Excel
;-
Procedure ExportXLS(ExcelFile.s)
Protected ADO_Connection.i, ADO_ConnectionString.s
Protected a$, b$, c$, d$, all$, tmp.i

  If FileSize(ExcelFile) > 0 
    DeleteFile(ExcelFile) 
  EndIf 

  ADO_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFile + ";Extended Properties=" + Chr(34) + "Excel 8.0" + Chr(34) 
  ADO_Connection = ADOmate_OpenDatabase(ADO_ConnectionString) 

  If ADO_Connection = 0 
    MessageRequester("Export XLS","Erreur: "+ADOmate_GetLastErrorDescription(),#MB_ICONERROR)
  EndIf 

  ;Create table
  ADOmate_DatabaseUpdate(ADO_Connection, "Create Table GroupeAD (Groupe String, Description String)");, Group_ID Integer, Attributs Integer)")

  If ADOmate_GetLastErrorDescription() <> "Okay."
    MessageRequester("Export XLS","Rapport Create Table: "+ADOmate_GetLastErrorDescription(),#MB_ICONERROR)
  EndIf

  tmp=0

  ForEach group_account()
    tmp + 1
    If tmp % 150 = 0
      Print(".")
    EndIf
    a$ = group_account()\name
    b$ = group_account()\comment
    c$ = Str(group_account()\group_id)
    d$ = Str(group_account()\attributes)
    all$ = "'" + a$ + "', '" + b$ + "'"

    ADOmate_DatabaseUpdate(ADO_Connection, "Insert Into GroupeAD (Groupe, Description) Values ("+all$+")")

    If ADOmate_GetLastErrorDescription() <> "Okay."
      PrintN("")
      PrintN("Erreur pendant l'export : ")
      PrintN("==> Insert into Table: "+all$)
      PrintN("==>"+ADOmate_GetLastErrorDescription())
      SetClipboardText(ADOmate_GetLastErrorDescription())
      ADOmate_CloseDatabase(ADO_Connection)
      End
    EndIf
    
  Next

  If ADOmate_GetLastErrorDescription() = "Okay."
    PrintN("")
    PrintN("Exportation réussie du fichier '"+ExcelFile+"'")
  EndIf

  ADOmate_CloseDatabase(ADO_Connection)
EndProcedure

OpenConsole()

Prototype.i _P_NetQueryDisplayInformation(*ServerName, Level.i, Index.i, EntriesRequested.i, PreferredMaximumLength.i, *ReturnedEntryCount, *SortedBuffer)
If OpenLibrary(0, "NETAPI32.DLL")
  NetQueryDisplayInformation._P_NetQueryDisplayInformation = GetFunction(0, "NetQueryDisplayInformation")
Else
  End
EndIf

If NetGetDCName_(0, 0, @*dcname) = 0
	dcname$ = PeekS(*dcname, -1, #PB_Unicode)

; or put the name of your PDC like :
; 		PokeS(*dcname, "PDC_01", -1, #PB_Unicode)
; 		dcname$ = PeekS(*dcname, -1, #PB_Unicode)
; ************************************

	If Left(dcname$, 2) = "\\"
		dcname$ = Mid(dcname$, 3)
	EndIf

  index = 0 : Total = 0

  t1 = ElapsedMilliseconds()
  Print("Patience...")

  Repeat
    result = NetQueryDisplayInformation(*dcname, 3, index, 1000, #MAX_PREFERRED_LENGTH, @ReturnedEntryCount, @*pBuf)
  
    If result = #ERROR_SUCCESS Or result = #ERROR_MORE_DATA
      Print(".")
      total + ReturnedEntryCount
      While ReturnedEntryCount > 0
        AddElement(group_account())
          gr_a$ = ReplaceString(PeekS(*pBuf\grpi3_name, -1, #PB_Unicode),"'","''")
          gr_b$ = ReplaceString(PeekS(*pBuf\grpi3_comment, -1, #PB_Unicode),"'","''")
          group_account()\name        = gr_a$
          group_account()\comment     = gr_b$
          group_account()\group_id    = *pBuf\grpi3_group_id
          group_account()\attributes  = *pBuf\grpi3_attributes
        index = *pBuf\grpi3_next_index
        *pBuf + SizeOf(NET_DISPLAY_GROUP)
        ReturnedEntryCount - 1
      Wend
    EndIf
    NetApiBufferFree_(*pBuf)
  Until result <> #ERROR_MORE_DATA
EndIf 
NetApiBufferFree_(*dcname)

PrintN("")
PrintN("Nombre de groupes = "+Str(total))
PrintN("")

PrintN("Création du fichier Excel...")
Print("Ajout des données dans le fichier...")

ExportXLS("AD_Group.xls")

PrintN("")

t2 = ElapsedMilliseconds() - t1

PrintN("Durée totale = "+Str(t2/1000)+" secondes")
Print("Frappez une touche pour quitter..."):Input()
CloseConsole()
“Fear is a reaction. Courage is a decision.” - WC