IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Lister les membres d'un groupe active directory


Sujet :

VBScript

  1. #1
    Membre régulier
    Profil pro
    ingé sys
    Inscrit en
    Avril 2004
    Messages
    156
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : ingé sys

    Informations forums :
    Inscription : Avril 2004
    Messages : 156
    Points : 70
    Points
    70
    Par défaut Lister les membres d'un groupe active directory
    Bonjour,
    aujourd'hui j'ai fait un ptit test pour le taf, lister les personnes qui appartiennent à un groupe.
    bien qu'ayant aucune notion en vbscript, j'ai testé quelques trucs et j'ai copié un script déjà existant mais ca passe pas.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    Function ObjSidToStrSid(arrSid)
    ' Function to convert OctetString (byte array) to Decimal string (SDDL) 
    Dim strHex, strDec
     
    strHex = OctetStrToHexStr(arrSid)
    strDec = HexStrToDecStr(strHex)
    ObjSidToStrSid = strDec
    End Function ' ObjSidToStrSid
     
    Function OctetStrToHexStr(arrbytOctet)
    ' Function to convert OctetString (byte array) to Hex string.
    Dim k
     
    OctetStrToHexStr = ""
    For k = 1 To Lenb(arrbytOctet)
    OctetStrToHexStr = OctetStrToHexStr _
    & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
    Next
    End Function ' OctetStrToHexStr
     
    Function HexStrToDecStr(strSid)
     
    Const BYTES_IN_32BITS = 4
    Const SRL_BYTE = 0
    Const IAV_START_BYTE = 2
    Const IAV_END_BYTE = 7
    Const RID_START_BYTE = 8
    Const MSB = 3 'Most significant byte
    Const LSB = 0 'Least significant byte
     
    Dim arrbytSid, lngTemp, base, offset, i
     
    ReDim arrbytSid(Len(strSid)/2 - 1)
     
    ' Convert hex string into integer array
    For i = 0 To UBound(arrbytSid)
    arrbytSid(i) = Cint("&H" & Mid(strSid, 2 * i + 1, 2))
    Next
     
    ' Add SRL number
    HexStrToDecStr = "S-" & arrbytSid(SRL_BYTE)
     
    ' Add Identifier Authority Value
    lngTemp = 0
    For i = IAV_START_BYTE To IAV_END_BYTE
    lngTemp = lngTemp * 256 + arrbytSid(i)
    Next
    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
     
    For base = RID_START_BYTE To UBound(arrbytSid) Step BYTES_IN_32BITS
    lngTemp = 0
    For offset = MSB to LSB Step -1
    lngTemp = lngTemp * 256 + arrbytSid(base + offset)
    Next
    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
    Next
    End Function ' HexStrToDecStr
     
    Const ForAppending = 8
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
        ("E:\altiris\eXpress\Deployment Server\AD_migration\scripts\liste_groupe_ad_test.txt", ForAppending, True)
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open "Provider=ADsDSOObject;"
    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
     
    objCommand.Properties("Page Size") = 100000
    objCommand.CommandText = _
        "<GC://CN=U_BSDE_PURCH_GROUPE,OU=Bonna Sabla,OU=Groups,dc=cns,dc=concrete,dc=int>;(objectCategory=Groups)" & _
            ";givenname,sn,userprincipalname,objectSid,distinguishedName,sAMAccountName;subtree"  
    Set objRecordSet = objCommand.Execute
    intCounter = 0
    Do Until objRecordset.EOF
     
        usergn=objRecordset.Fields("givenName")
        userna=objRecordset.Fields("sn")
        usermail=objRecordset.Fields("userprincipalname")
        userdn=objRecordset.Fields("distinguishedName")
        useracc=objRecordset.Fields("sAMAccountName")
     
        strsid=ObjSidToStrSid(cstr(objRecordset.Fields("objectSid")))
        WScript.Echo VbCrLf & intCounter & " dans le groupe"	
        intCounter = intCounter + 1
     
        objTextFile.WriteLine(usermail)
        objRecordset.MoveNext
     
    Loop
     
    WScript.Echo VbCrLf & intCounter & " utilisateur(s) dans le groupe"
     
    objTextFile.Close 
    objConnection.Close
    Il y a surement des choses inutiles dans ce code, je n'ai fais que le reprendre en changeant juste ce qui m'interesse.
    Au départ ce script était fais pour chopé les infos utilisateurs d'une OU de l'OU User account

    Moi il faut que je chope les noms des personnes qui sont dans une OU de l'OU de Groups.

    Je crois que les infos suivantes sont pas adaptées :
    Et aussi, je crois qu'il ne rentre dans pas dans ma boucle, il sort toujours " personnes dans le groupe".
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    objCommand.CommandText = _
        "<GC://CN=U_BSDE_PURCH_GROUPE,OU=Bonna Sabla,OU=Groups,dc=cns,dc=concrete,dc=int>;(objectCategory=Groups)" & _
            ";givenname,sn,userprincipalname,objectSid,distinguishedName,sAMAccountName;subtree"
    qqn peut-il me donner les bonnes infos ? ou m'éclairer un peu ?

    Merci d'avance !

  2. #2
    Membre averti
    Inscrit en
    Août 2007
    Messages
    302
    Détails du profil
    Informations personnelles :
    Âge : 57

    Informations forums :
    Inscription : Août 2007
    Messages : 302
    Points : 341
    Points
    341
    Par défaut
    Salut,

    tu peux essayer ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
     
    On Error Resume next
     
    Function ObjSidToStrSid(arrSid)
    ' Function to convert OctetString (byte array) to Decimal string (SDDL) 
    Dim strHex, strDec
     
    strHex = OctetStrToHexStr(arrSid)
    strDec = HexStrToDecStr(strHex)
    ObjSidToStrSid = strDec
    End Function ' ObjSidToStrSid
     
    Function OctetStrToHexStr(arrbytOctet)
    ' Function to convert OctetString (byte array) to Hex string.
    Dim k
     
    OctetStrToHexStr = ""
    For k = 1 To Lenb(arrbytOctet)
    OctetStrToHexStr = OctetStrToHexStr _
    & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
    Next
    End Function ' OctetStrToHexStr
     
    Function HexStrToDecStr(strSid)
     
    Const BYTES_IN_32BITS = 4
    Const SRL_BYTE = 0
    Const IAV_START_BYTE = 2
    Const IAV_END_BYTE = 7
    Const RID_START_BYTE = 8
    Const MSB = 3 'Most significant byte
    Const LSB = 0 'Least significant byte
     
    Dim arrbytSid, lngTemp, base, offset, i
     
    ReDim arrbytSid(Len(strSid)/2 - 1)
     
    ' Convert hex string into integer array
    For i = 0 To UBound(arrbytSid)
    arrbytSid(i) = Cint("&H" & Mid(strSid, 2 * i + 1, 2))
    Next
     
    ' Add SRL number
    HexStrToDecStr = "S-" & arrbytSid(SRL_BYTE)
     
    ' Add Identifier Authority Value
    lngTemp = 0
    For i = IAV_START_BYTE To IAV_END_BYTE
    lngTemp = lngTemp * 256 + arrbytSid(i)
    Next
    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
     
    For base = RID_START_BYTE To UBound(arrbytSid) Step BYTES_IN_32BITS
    lngTemp = 0
    For offset = MSB to LSB Step -1
    lngTemp = lngTemp * 256 + arrbytSid(base + offset)
    Next
    HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
    Next
    End Function ' HexStrToDecStr
    Const ForAppending = 8
    Const ADS_SCOPE_SUBTREE = 2
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile("liste_groupe_ad_test.txt", ForAppending, True)
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open "Provider=ADsDSOObject;"
    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
     
    objCommand.Properties("Page Size") = 100000
    objCommand.CommandText = "SELECT givenName,sn,userprincipalname,distinguishedName,sAMAccountName,objectSid FROM 'LDAP://CN=U_BSDE_PURCH_GROUPE,OU=Bonna Sabla,OU=Groups,dc=cns,dc=concrete,dc=int' WHERE objectCategory='group'"
    Set objRecordSet = objCommand.Execute
     
    objRecordset.MoveFirst
    Do Until objRecordset.EOF
     
        usergn=objRecordset.Fields("givenName")
        userna=objRecordset.Fields("sn")
        usermail=objRecordset.Fields("userprincipalname")
        userdn=objRecordset.Fields("distinguishedName")
        useracc=objRecordset.Fields("sAMAccountName")
     
        strsid=ObjSidToStrSid(cstr(objRecordset.Fields("objectSid")))
     
        strLineF = strLineF & userdn & ";" & useracc & ";" & strsid & vbcrlf
        WScript.Echo userdn & ";" & useracc & ";" & strsid
     
        Set objGroup = GetObject("LDAP://" & userdn) 
        For Each strUser in objGroup.Member
            Set objUser =  GetObject("LDAP://" & strUser)
            ReDim Preserve arrNames(intSize)
            arrNames(intSize) = objUser.CN
            intSize = intSize + 1
            strLineF = strLineF & " -> " & objUser.CN &";" & objUser.SN & ";" & objUser.mail & vbCrLf
            WScript.Echo " -> " & objUser.CN &";" & objUser.SN & ";" & objUser.mail
        Next
     
        objRecordset.MoveNext
     
    Loop
     
    WScript.Echo VbCrLf & objRecordset.RecordCount & " utilisateur(s) dans le groupe"
     
    objTextFile.WriteLine strlineF 
    objTextFile.Close 
    objConnection.Close
     
    WScript.Quit
     
    Function ObjSidToStrSid(arrSid)
    ' Function to convert OctetString (byte array) to Decimal string (SDDL) 
    Dim strHex, strDec
     
    strHex = OctetStrToHexStr(arrSid)
    strDec = HexStrToDecStr(strHex)
    ObjSidToStrSid = strDec
    End Function ' ObjSidToStrSid
    C'est vite fait sans chercher à optimiser, mais on ne devrait pas etre bien loin de la solution.

    A++
    Plus tu pédales moins vite, moins t'avances plus vite.

Discussions similaires

  1. [Débutant] Lister les utilisateur d'un groupe ACTIVE DIRECTORY
    Par FromHell04 dans le forum VB.NET
    Réponses: 0
    Dernier message: 01/02/2012, 18h23
  2. Réponses: 0
    Dernier message: 09/05/2011, 15h30
  3. Réponses: 12
    Dernier message: 16/03/2011, 14h07
  4. Vbs AD Lister les membres de groupe imbriqué
    Par vikingraver dans le forum VBScript
    Réponses: 2
    Dernier message: 16/11/2009, 15h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo