Jump to content

Using AutoIt to generate a list of users in a group in Active Directory


Recommended Posts

HELP! I am fairly new to the advanced features of AutoIt. I have used it to automate some Software installs, perform Password changes (using the SEND command), etc.

I have now been charged with writing a script that will:

1. Generate a list of users in a group in Active Directory

2. For each user in that group, use the "net send" dos command to send a specific broadcast message.

I think I can handle the second part of this (as I have used AutoIt to perform DOS functions, and have used for...next loops).

I just need help with the first part. We have different groups set up for different departments. We would like to be able to send a broadcast message to one specific group. Can anyone help me? Thanks in advance.

Link to comment
Share on other sites

Welcome to the forum.

Take a look at:

http://www.autoitscript.com/forum/index.ph...st&p=144056

and

http://www.autoitscript.com/forum/index.ph...st&p=201634

Caveat: I've not used either script.

-MSP-

Thanks HereWasPlato...

I checked both of these out, and the second one will not help me, as it is asking for the username... I need to automatically fetch the name from a group. The first script seemed promising. I may have been able to use part of it, however, I do not speak French, and cannot make out what he is trying to do... :)

Here is the pseudo code for the entire script I have in mind...

***************************************************************

GET MESSAGE TEXT

$Message = Inputbox(MESSAGE)

GET USERID from AD Group <<This is what I don't know how to do...>>

For Each USERID do

$Username = LANID from ActiveDirectory

RUN COMMAND LINE : net send $Username $Message

Next

***************************************************************

I may have to try creating a VB script... (although I would have to learn VBS first...)

Link to comment
Share on other sites

@EarleKelley

Maybe this can help you out.

$objGroup = ObjGet("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
$objGroup.GetInfo
 
$arrMemberOf = $objGroup.GetEx("member")
 
ConsoleWrite ("Members:")
For $strMember in $arrMemberOf
    ConsoleWrite( $strMember & @CRLF & @CRLF)
Next

Regards

ptrex

Link to comment
Share on other sites

@EarleKelley

Maybe this can help you out.

$objGroup = ObjGet("LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com")
$objGroup.GetInfo
 
$arrMemberOf = $objGroup.GetEx("member")
 
ConsoleWrite ("Members:")
For $strMember in $arrMemberOf
    ConsoleWrite( $strMember & @CRLF & @CRLF)
Next

Regards

ptrex

How do you run this against the server or do you need to run it on the server - by the way I like the Exchange script that you did, but that too had to be ran on the server. Maybe a permission issue?

EDIT Actually - I get an error on second line $objGroup.GetInfo - maybe issue with server name?

Edited by nitekram

2¢

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

  • Developers

How do you run this against the server or do you need to run it on the server - by the way I like the Exchange script that you did, but that too had to be ran on the server. Maybe a permission issue?

This will work on your own pc as long as you are logged-on in the domain and have admin rights.

:)

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This will work on your own pc as long as you are logged-on in the domain and have admin rights.

:)

I get this

>Running:(3.2.2.0):C:\Program Files\AutoIt3\autoit3.exe "Z:\New 251507\user_list_AD.au3"

Z:\New 251507\user_list_AD.au3 (6) : ==> Variable must be of type "Object".:

$objGroup.GetInfo

$objGroup^ ERROR

either running it on the server or work station - admin account on both

2¢

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

  • Developers

I get this

>Running:(3.2.2.0):C:\Program Files\AutoIt3\autoit3.exe "Z:\New 251507\user_list_AD.au3"

Z:\New 251507\user_list_AD.au3 (6) : ==> Variable must be of type "Object".:

$objGroup.GetInfo

$objGroup^ ERROR

either running it on the server or work station - admin account on both

You are sure you specified the correct domain information on the first line ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

For your group-listing pleasure:

#include <array.au3>

$GroupPath = InputBox("List Group Members", "Enter DOMAIN/GROUP to list: ")

$avUsers = _GroupGetMembers($GroupPath)
If Not @error Then
    _ArrayDisplay($avUsers, "Users in group")
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)
EndIf


; ------------------------------------------------------------
; Function _GroupGetMembers($sPath)
;   Call with:  _GroupGetMembers($sPath)
;       Where:  $sPath = DOMAIN/GROUP, i.e. "MyDomain/Domain Admins"
;           If Domain is not included, uses local machine, i.e. "Administrators" will be xlated to "./Administrators"
;   On success returns an array of members in the specified group with [0] = count.
;       For an empty group, returns [0] = 0.
;   On failure sets @error.
; ------------------------------------------------------------
Func _GroupGetMembers($sPath)
    Local $oUser, $sRET = "", $avRET
    
    ; Check path to group
    $sPath = StringReplace($sPath, "\", "/") ; Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath ; Use local if no domain given
    
    ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
        ; List users in group
        For $oUser In $oGroup.Members
            $sRET &= $oUser.Name & @LF
        Next
        ; Split into an array for return
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
        ; Change result for empty group [0] = 0
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
        ; Error getting group object
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GroupGetMembers

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You are sure you specified the correct domain information on the first line ?

I did not do that - can you explain what I need to change - sorry all I know is that DC is domain controller - right?

Edit

"LDAP://cn=Scientists,ou=R&D,dc=NA,dc=fabrikam,dc=com"

1147 05/16/07

Found some more info - I was wrong ... WOW ... like that will never happen again

DC is domain component

If an organization has domain name foo.example, its top level LDAP entry will therefore typically have the DN dc=foo,dc=example (where dc means domain component).

Edited by nitekram

2¢

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

For your group-listing pleasure:

#include <array.au3>

$GroupPath = InputBox("List Group Members", "Enter DOMAIN/GROUP to list: ")

$avUsers = _GroupGetMembers($GroupPath)
If Not @error Then
    _ArrayDisplay($avUsers, "Users in group")
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)
EndIf
; ------------------------------------------------------------
; Function _GroupGetMembers($sPath)
;   Call with:  _GroupGetMembers($sPath)
;       Where:  $sPath = DOMAIN/GROUP, i.e. "MyDomain/Domain Admins"
;           If Domain is not included, uses local machine, i.e. "Administrators" will be xlated to "./Administrators"
;   On success returns an array of members in the specified group with [0] = count.
;       For an empty group, returns [0] = 0.
;   On failure sets @error.
; ------------------------------------------------------------
Func _GroupGetMembers($sPath)
    Local $oUser, $sRET = "", $avRET
    
    ; Check path to group
    $sPath = StringReplace($sPath, "\", "/") ; Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath ; Use local if no domain given
    
    ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
        ; List users in group
        For $oUser In $oGroup.Members
            $sRET &= $oUser.Name & @LF
        Next
        ; Split into an array for return
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
        ; Change result for empty group [0] = 0
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
        ; Error getting group object
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GroupGetMembers

:)

Thanks PasltyDS.... This worked!

Zedna, I will check yous out in a bit.. Thanks all for your help!

Link to comment
Share on other sites

  • 4 months later...

Thanks PasltyDS.... This worked!

Zedna, I will check yous out in a bit.. Thanks all for your help!

i've tried this code and while it works for some objects, some report an error. we use a NDS tree that replicates to the AD. Preferably i would like to query the NDS but, the AD should return the results needed. can any one explain why i'm not getting consistent results and perhaps provide a way around the problem.

the objects i'm searching are in the same container.

Cheers

Link to comment
Share on other sites

  • 1 year later...

For your group-listing pleasure:

#include <array.au3>

$GroupPath = InputBox("List Group Members", "Enter DOMAIN/GROUP to list: ")

$avUsers = _GroupGetMembers($GroupPath)
If Not @error Then
    _ArrayDisplay($avUsers, "Users in group")
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)
EndIf


; ------------------------------------------------------------
; Function _GroupGetMembers($sPath)
;   Call with:  _GroupGetMembers($sPath)
;       Where:  $sPath = DOMAIN/GROUP, i.e. "MyDomain/Domain Admins"
;           If Domain is not included, uses local machine, i.e. "Administrators" will be xlated to "./Administrators"
;   On success returns an array of members in the specified group with [0] = count.
;       For an empty group, returns [0] = 0.
;   On failure sets @error.
; ------------------------------------------------------------
Func _GroupGetMembers($sPath)
    Local $oUser, $sRET = "", $avRET
    
    ; Check path to group
    $sPath = StringReplace($sPath, "\", "/") ; Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath ; Use local if no domain given
    
    ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
        ; List users in group
        For $oUser In $oGroup.Members
            $sRET &= $oUser.Name & @LF
        Next
        ; Split into an array for return
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
        ; Change result for empty group [0] = 0
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
        ; Error getting group object
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GroupGetMembers

:)

Hello PsaltyDS

I like your code.

I have a exchange 2007 and I would like do you know if it's possible to return value in email, phone, faxnumber and other value of AD ??

thank you

Link to comment
Share on other sites

Hello PsaltyDS

I like your code.

I have a exchange 2007 and I would like do you know if it's possible to return value in email, phone, faxnumber and other value of AD ??

thank you

Ought to be $oUser.mail, $oUser.telephoneNumber, $oUser.facsimileTelephoneNumber, etc. as listed under Active Directory user properties on MSDN.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Well, I'm don't understand why, but I changer ouser for displayname or email, and I receive allways same result.

my goal, it's to make graphic with all info of user (email, street ...)

#include <array.au3>

$GroupPath = "agri-marche\domain users"

$displayName = _GroupGetMembers($GroupPath)
If Not @error Then
    _ArrayDisplay($displayName, "Users in group")
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)
EndIf


; ------------------------------------------------------------
; Function _GroupGetMembers($sPath)
;   Call with:  _GroupGetMembers($sPath)
;    Where: $sPath = DOMAIN/GROUP, i.e. "MyDomain/Domain Admins"
;      If Domain is not included, uses local machine, i.e. "Administrators" will be xlated to "./Administrators"
;   On success returns an array of members in the specified group with [0] = count.
;    For an empty group, returns [0] = 0.
;   On failure sets @error.
; ------------------------------------------------------------
Func _GroupGetMembers($sPath)
    Local $displayName, $sRET = "", $avRET
    
   ; Check path to group
    $sPath = StringReplace($sPath, "\", "/"); Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath; Use local if no domain given
    
   ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
       ; List users in group
        For $displayName In $oGroup.Members
            $sRET &= $displayName.Name & @LF
        Next
       ; Split into an array for return
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
       ; Change result for empty group [0] = 0
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
       ; Error getting group object
        Return SetError(1, 0, 0)
    EndIf
EndFunc  ;==>_GroupGetMembers
Link to comment
Share on other sites

Well, I'm don't understand why, but I changer ouser for displayname or email, and I receive allways same result.

my goal, it's to make graphic with all info of user (email, street ...)

Not sure what you thought was going to be different just by changing the name of the variable. What I meant was more like:
; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
      ; List users in group
        For $oUser In $oGroup.Members
            $sRET &= $oUser.Name & "," & $oUser.mail & "," & $oUser.telephoneNumber & "," & $oUser.facsimileTelephoneNumber & @LF
        Next

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 2 months later...

Not sure what you thought was going to be different just by changing the name of the variable. What I meant was more like:

; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
     ; List users in group
        For $oUser In $oGroup.Members
            $sRET &= $oUser.Name & "," & $oUser.mail & "," & $oUser.telephoneNumber & "," & $oUser.facsimileTelephoneNumber & @LF
        Next

:)

When I run this, I get the following error in Scite:

+>09:01:55 Starting AutoIt3Wrapper v.1.10.1.12  Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X86  ANSI)
>Running AU3Check (1.54.13.0)  from:d:\Program Files\AutoIt3
+>09:01:55 AU3Check ended.rc:0
>Running:(3.2.12.1):d:\Program Files\AutoIt3\autoit3.exe "C:\autoit\adgrouplist.au3"    
C:\autoit\adgrouplist.au3 (37) : ==> The requested action with this object has failed.:
$sRET &= $oUser.Name & "," & $oUser.mail & "," & $oUser.telephoneNumber & "," & $oUser.facsimileTelephoneNumber & @LF
$sRET &= $oUser.Name & "," & $oUser.mail ^ ERROR
->09:01:55 AutoIT3.exe ended.rc:1
+>09:01:56 AutoIt3Wrapper Finished
>Exit code: 1   Time: 1.661

checking with ldap browser, I see the proper terms are being used (ie. .name, .mail, .telephonenumber), but .name is the only one that works. Any ideas?

Thanks

Link to comment
Share on other sites

When I run this, I get the following error in Scite:

+>09:01:55 Starting AutoIt3Wrapper v.1.10.1.12  Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X86  ANSI)
>Running AU3Check (1.54.13.0)  from:d:\Program Files\AutoIt3
+>09:01:55 AU3Check ended.rc:0
>Running:(3.2.12.1):d:\Program Files\AutoIt3\autoit3.exe "C:\autoit\adgrouplist.au3"    
C:\autoit\adgrouplist.au3 (37) : ==> The requested action with this object has failed.:
$sRET &= $oUser.Name & "," & $oUser.mail & "," & $oUser.telephoneNumber & "," & $oUser.facsimileTelephoneNumber & @LF
$sRET &= $oUser.Name & "," & $oUser.mail ^ ERROR
->09:01:55 AutoIT3.exe ended.rc:1
+>09:01:56 AutoIt3Wrapper Finished
>Exit code: 1   Time: 1.661

checking with ldap browser, I see the proper terms are being used (ie. .name, .mail, .telephonenumber), but .name is the only one that works. Any ideas?

Thanks

Add a COM error handler and see what the exact error is when it fails. I'm thinking some properties may require more rights than others, and you may get access denied to more personal information.

:)

P.S. In the future, just start a new topic for your issue. Reopening old topics with new questions just makes it harder for people to find answers with a search. There is no marginal cost for new topics!

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...